Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在AJAX中调用JSON成功_Javascript_Jquery_Json_Ajax - Fatal编程技术网

Javascript 在AJAX中调用JSON成功

Javascript 在AJAX中调用JSON成功,javascript,jquery,json,ajax,Javascript,Jquery,Json,Ajax,我想在我的AJAX中调用json数据。我对处理json和AJAX还是新手。有人能帮助我如何访问来自另一个URL的json数据吗?我想将id与JSON数据进行比较。以下是我目前的代码: function getCard(id){ $.ajax({ type: "GET", data: "id=" + id, success: function(data){

我想在我的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?

                       }
         });
    }

是的,祖玛,您可以在Ajax调用的成功函数中调用另一个Ajax。下面是一个例子:

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

            });
        });
});
功能卡(id){


此代码将适用于您

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

你为什么不试试呢?我会使用回调函数来保持一切干净。这是可能的。尝试嵌套另一个
$.ajax
调用。你的url在哪里?如果服务器端逻辑正确,你的json将是传递给success函数的参数,在本例中是
data
    $.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
            }
        });