0

Web サービスを介して取得した Json の結果があり、それが機能するかどうかを確認したいのですが、Json の値をボタンまたはリンクに表示するにはどうすればよいですか? そしてページの読み込み時?

返される値の 1 つが次のようになります: "name": "Muhammad Ali", "nickname": "The Greatest",

json と javacascript は初めてです。

Javascript と json :

    function Getdata() {
    $.ajax({
        type: "POST",
        data: "{}",
        url: "https://raw.github.com/appcelerator/Documentation-Examples/master/HTTPClient/data/json.txt",
        contentType: "application/json; cherset=utf-8",
        datatype: "json",
        success: loadpage,
        failure: givealert
    });



    function loadpage(result) {
        if (resu.hasOwnProperty("d")) { result = res.d; }
        var data = JQuery.parseJSON(result);
    }

    function givealert(error) {
        alert('failed!!' + error);
    }
}

ラベルとフォームのロード時に Web サービスから 1 つの値を表示するにはどうすればよいですか? label/button の html マークアップ:

 <div id="listheight">
                <a type="button" id="routing" href="#datatapage"></a>
            </div>

私は cordova/phonegap、Visual studi2010、html、javascript、css、jquerymobile、jquery1.8.2 を使用しています。

前もって感謝します!

4

1 に答える 1

1

まず、この HTML を使用します。

<a type="button" id="routing" href="#datatapage" onclick="Getdata()">Click</a> 

このコードを使用して、ページ読み込み時にメソッドを自動的に呼び出します

$(document).ready(function() {
  // this is executed on page load
  Getdata();
});

コードを次のように変更します

jQuery.support.cors = true;

function Getdata() {
  var request = $.ajax({
    type: "POST",
    data: "{}",
    dataType: 'json',
    url: "data/json.txt",  // better use a relative url here
    mimeType: "application/json; cherset=utf-8",
    success: loadpage,
    failure: givealert
  });

  function loadpage(result) {
    // this only displays you the values in a messagebox for you to check if it works
    // you can remove the following two lines
    alert("Name = "+result.fighters[0].name);
    alert("Nickname = "+result.fighters[0].nickname);
    // this changes the text
    document.getElementById("routing").innerHTML = result.fighters[0].name;
  }

  function givealert(error) {
    alert('failed!!' + error);
  }
}

このための JSFiddle を作成しました: http://jsfiddle.net/FUWyJ/ リクエストを送信する URL は JSFiddle によって提供され、コピーを作成したhttps://gist.github.com/4001105を指していることに注意してください。あなたのサンプルデータ。

于 2012-11-01T07:53:23.817 に答える