簡體   English   中英

在AJAX成功中調用JSON

[英]Call JSON inside AJAX success

我想在我的AJAX成功中調用json數據。 我仍然是操縱json和AJAX的新手。 有人可以幫助我如何訪問來自其他URL的json數據嗎? 我想將id與JSON數據進行比較。 到目前為止,這是我的代碼:

function getCard(id){

        $.ajax({
              type: "GET",
              data: "id=" + id,
              success: function(data){

                        #call JSON data here from another URL
                        # Is it possible to call another AJAX here?

                       }
         });
    }

是的Zuma你可以在Ajax調用的Success函數中調用另一個Ajax。 以下是示例:

$.ajax({
        type: "post",
        url: url1,
        data: data1,
        success: function(data){                
            $.ajax({
                type: "post",
                url: url2,
                data: data2,
                success: function(data){

            });
        });
});

function getCard(id){

    $.ajax({
          type: "Get",
          url: "發送請求的地址",
          data: "id=" + id,
          success: function(data){

                    #call JSON data here from another URL
                    # if success,you can call another AJAX.
                    # Is it possible to call another AJAX here?
                    # yes!

                   }
     });
}

此代碼適合您

    $.ajax({
            url: "url",
            method: "post",
            data: "id=" + id,
            success:function(data) {
                // success goes here
                  $.ajax({
                       url: "url",
                       async:false,
                       method: "post",
                      data: "id=" + id,
                           success:function(json) {
                                   JSON.parse(json);

                                  // compare data and json here
                          },
                         error: function(){
                        // error code goes here
                       }
                }); 
            },
            error: function(){
                // error code goes here
            }
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM