2016-03-19 5 views
0

私はJavasciptを学習する初心者です。私がしたいのは、JavaScriptコードを使ってKohaライブラリシステムからデータを取得することです。 Kohaは、Apache2 WebサーバーとMySQLデータベースを使用してカタログを格納します。私はkoha(An ILS)からデータを取得するためのjavascriptコードを書いています

私のコードはでした:

<!DOCTYPE html> 
<html> 
<body> 

<div id="demo"><h2>Result</h2></div> 


<script> 
    var xhttp = new XMLHttpRequest(); 
    xhttp.open("GET","http://127.0.0.1:8080/cgi-bin/koha/svc/bib/3?userid=user&password=user1", false); 
    xhttp.send(); 

    xmlDoc = xhttp.responseXML; 
    txt = ""; 
    x = xmlDoc.getElementsByTagName("a"); 
    for (i = 0; i < x.length; i++) { 
    txt += x[i].childNodes[0].nodeValue + "<br>"; 
    } 
    document.getElementById("demo").innerHTML = txt; 
</script> 

</body> 
</html> 

それは仕事をdoesntの。

私はURLを入力するときは:私のブラウザで

http://127.0.0.1:8080/cgi-bin/koha/svc/bib/3?userid=user&password=user1

を私はJavaScriptを使用して出力を取得したいこの

This XML file does not appear to have any style information associated with it. The document tree is shown below. 

    <record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.loc.gov/MARC21/slim" xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"> 
    <leader>00099nam a22000617a 4500</leader> 
    <datafield tag="020" ind1=" " ind2=" "> 
    <subfield code="a">234234</subfield> 
    </datafield> 
    <datafield tag="245" ind1=" " ind2=" "> 
    <subfield code="a">Harry Potter</subfield> 
    </datafield> 
    <datafield tag="999" ind1=" " ind2=" "> 
    <subfield code="c">3</subfield> 
    <subfield code="d">3</subfield> 
    </datafield> 
    </record> 

を取得します。助けてください!

+0

あなたが可能に使用できるjQueryの何かか?ネイティブではなく、jQueryでAJAXリクエストを行う方がずっと簡単です。 –

+0

コード全体を入力していただきありがとうございます。それがより良い選択肢なら、私はjQueryに固執するでしょう。コードは、表示されたデータをxmlファイルとしてローカルに保存したときに機能しました。私が欲しいのは、Kohaのsvc http APIを使って、URL: '127.0.0.1:8080/cgi-bin/koha/svc/bib/...'からkohaサーバーからデータを取得することです。このリンクは、wiki.koha-community.org/wiki/Koha_/svc/_HTTP_APIのドキュメントを提供しています。 – Blue

答えて

0

jQueryがオプションの場合は、ajax呼び出しとXML処理の両方に使用することをお勧めします。 xmlの結果をファイルとしてローカルに "Temp.xml"として保存し、ローカルWebサーバー上に下のページを作成しました。 $.get("Temp.xml")呼び出しは、ファイルをXMLドキュメントとしてメモリにプルするajax呼び出しです。そこから、私は自分が望むノードを見つけてそれらをページに書き込むためにユーザーjQueryを使います。 (document.writeはおそらくページのデータを取得するための最良の方法ではありませんが、それはデモの目的のために仕事を取得します。)

<!DOCTYPE html> 
<html> 
<body> 

<div id="demo"><h2>Result</h2></div> 

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.js"></script> 
<script> 
    // Get the xml via a "GET" request. When it's done, process the results. 
    $.get("Temp.xml").done(function(xmlDoc) { 
     // The resulting xml is basically a bunch of datafield nodes that contain 
     // subfield nodes. Write each datafield values and then each of their 
     // subfield values. 
     $(xmlDoc).find("datafield").each(function(index, datafield) { 
      // getAttribute can be used on native elements to get their attribute values... 
      document.write('datafield[tag] = ' + datafield.getAttribute('tag')); 
      document.write('<br>'); 

      // $(...) wraps an element with jQuery functionality and allows you quickly 
      // and easily find/process particular notes. 
      $(datafield).find("subfield").each(function(index, subfield) { 
       document.write(' &nbsp; &nbsp; '); // for indentation 
       document.write('subfield[code] = ' + subfield.getAttribute('code')); 
       document.write(', '); // for indentation 
       document.write('subfield = ' + subfield.textContent); 
       document.write('<br>'); 
      }); 
     }); 
    }); 
</script> 

</body> 
</html> 
+0

コード全体を入力していただきありがとうございます。それがより良い選択肢なら、私はjQueryに固執するでしょう。コードは、表示されたデータをxmlファイルとしてローカルに保存したときに機能しました。私が欲しいのは、Kohaのsvc http APIを使用して、URLからkohaサーバーからデータを取得することです: 'http://127.0.0.1:8080/cgi-bin/koha/svc/bib/3?userid=user&password=user1' 。このリンクは、https://wiki.koha-community.org/wiki/Koha_/svc/_HTTP_APIのドキュメントを提供しています。 – Blue

+0

$ .getのパラメータは、WebサービスのURLです。したがって、$ .get( "Temp.xml")を$ .get( "127.0.0.1:8080/cgi-bin/koha/svc/bib")に変更するか、Webサービスが何であれ変更してください。私はkohaをローカルで実行していないので、私のコード例はそれを含んでいませんでしたが、それはあなたのために働くはずです。 –

+0

私はそれを試して、それはその後仕事をしませんでした。しかし、後で、私のブラウザがクロスオリジンのリソース共有を許可していないことが判明しました。私はクロームCORS拡張を追加し、それは働いた!コードと説明をありがとうございました。私は今jQueryに入っています。 :) – Blue

関連する問題