0

ここで尋ねた以前の質問からの回答は、非同期呼び出しについてますます学んでいるため、別の問題を引き起こしました。(前の回答が示したように)保存できるリストを達成する方法がまだわかりません。選択したリスト項目のデータを使用します。

$('#home').live('pageshow', function(){
    // creating object 
    var customerList = JSON.parse('{"customerInAccount":[{"customer_name":"Jane Doe","auto_id":"21"},{"customer_name":"Jack   Black","auto_id":"22"}]}');

    // creating html string
    var listString = '<ul data-role="listview" id="customerList">';

    // running a loop
    $.each(customerList.customerInAccount, function(index,value){
     listString += '<li><a href="#" data-cusid='+this.auto_id+'>'+this.customer_name+'</a></li>';
    });
    listString +='</ul>';

    console.log(customerList);

    //appending to the div
    $('#CustomerListDiv').html(listString);

    // refreshing the list to apply styles
    $('#CustomerListDiv ul').listview();

    // getting the customer id on the click
    $('#customerList a').bind('click',function(){
     var customerID = $(this).data('cusid');  
     alert(customerID);
    });
});

jsフィドルでhttp://jsfiddle.net/amEge/3/

このコードはうまく機能し、目的を達成することができますが、最初に ajax 呼び出しから customerList を設定する必要があります。しかし、「成功」機能からは、コードを機能させることができないようです。

$.post(postTo,{id:idVar} , function(data) {

    customerList = data;
    //alert(customerList);
})

コードを ajax 関数内に移動すると、機能しません。誰かが私を助けて、非同期呼び出しからこれを処理する方法を教えてくれるかどうか疑問に思っていましたか?

どうもありがとう

4

2 に答える 2

1

以下のようにページを変更する必要があります。

// I assume this is your dot net web service url
var webServiceURL = 'customer.asmx/GetCustomer';

// here home is your page's ID
$('#home').live('pageshow', function(){
    getCustomerList();

});

function getCustomerList(){
    param=JSON.strigify({id:'2'});
    callWebService(param, webServiceURL, onGetCustListSuccess, onGetCustListFailed)
}

function onGetCustListFailed(){
    alert("service request failed");
}

function onGetCustListSuccess(data, status){
    // creating object 
    // replace previous line with below
    // var customerList = JSON.parse('{"customerInAccount":[{"customer_name":"Jane Doe","auto_id":"21"},{"customer_name":"Jack   Black","auto_id":"22"}]}');
    var customerList = JSON.parse(data.d);

    // creating html string
    var listString = '<ul data-role="listview" id="customerList">';

    // running a loop
    $.each(customerList.customerInAccount, function(index,value){
     listString += '<li><a href="#" data-cusid='+this.auto_id+'>'+this.customer_name+'</a></li>';
    });
    listString +='</ul>';

    console.log(customerList);

    //appending to the div
    $('#CustomerListDiv').html(listString);

    // refreshing the list to apply styles
    $('#CustomerListDiv ul').listview();

    // getting the customer id on the click
    $('#customerList a').bind('click',function(){
     var customerID = $(this).data('cusid');  
     alert(customerID);
    });
}

function callWebService(param,url,successFunc,errorFunc){
    if(errorFunc=='undefined'){
        errorFunc=OnDataError;
    } 
    $.ajax({            
            type: "POST",
            url: url,
            data: param,
            contentType:"application/json; charset=utf-8",
            dataType: "json",
            success: successFunc,
            error: errorFunc,
            beforeSend:function(){$.mobile.loading( 'show' );},
            complete:function(){$.mobile.loading( 'hide');}
    });
}

これがあなたを助けることを願っています。問題がある場合は、ここで私に尋ねてください。

于 2013-03-05T04:45:47.917 に答える
0

間違っている場合は訂正してください。ただし、「ライブ」コードは次のようになると推測できます。

$('#home').live('pageshow', function(){
   // creating object 
   var customerList;

   $.post(postTo, {id:idVar}, function(data) {
      customerList = data;
      //alert(customerList);
   });

   // creating html string
   var listString = '<ul data-role="listview" id="customerList">';

   // and all the rest...

その場合、問題は、入力されている customerList 変数に依存するコード (「残りはすべて...」) が、HTTP 要求からの応答が Web サーバーから返されるのを待つのではなく、すぐに実行されることです。$.post() は、HTTP トランザクションが Web サーバーとの間を行き来する間、待機しません (または、コンピューター ソフトウェア プログラミング ゲームで言うように「ブロック」します)。代わりに、コードの残りの部分がすぐに実行され、その後、その応答がブラウザーに返されたときに、JavaScript エンジンが成功関数 (または「クロージャー」、つまり「クロージャー」) を忠実に実行します。

したがって、この他のすべてのコード (customerList に依存するもの) を別の関数に入れ、成功クロージャー内からその関数を呼び出す必要があります。その場合、customerList 変数も必要ありません。新しい応答データを引数として新しい関数に渡すだけです。

于 2013-03-05T04:50:46.593 に答える