简体   繁体   中英

Call JSON inside AJAX success

I want to call a json data inside my AJAX success. I'm still new on manipulating json and AJAX. Can somebody help me on how to access my json data which is from another URL? And I want to compare the id to the JSON data. Here's my code so far:

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?

                       }
         });
    }

yes Zuma you can call another Ajax inside Success function of Ajax call. Below is the example:

$.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!

                   }
     });
}

This code will work for you

    $.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
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM