2015-01-20 95 views
0

发送到某些 'request_url' HTTP请求返回格式{ '更迭':1 'HTML': 'thestuff'} JSON响应获取JSON嵌套AJAX调用

所以当

jQuery.ajax({ 
    url: 'request_url', 
    success: function(response){ 
     alert(response.html); //'thestuff' is here as expected 
    } 
}); 

'thestuff'可以在预期的response.html中找到。但如果这个ajax在另一个ajax请求的'成功'回调中被调用,那么response.html将变空,'thestuff'将变为'响应'。

jQuery.ajax({ 
    url: 'some_other_url', 
    success: function(some_other_response){ 
     jQuery.ajax({ 
     url: 'request_url', 
     success: function(respose){ 
      alert(response.html); //there is nothing 
      alert(response);   //I see 'thestuff' which is expected in 'html' 
     } 
    }) 
    } 
}); 

为什么会发生?

更新:“thestuff”包含了一些JS代码为{}我想的东西可能会不知所措,但它为什么单(不嵌套)Ajax请求效果很好。

+0

无数错别字VS'response' VS'respose'。很难知道什么是。建议你在每个成功处理程序中以不同的方式命名它们,同时调试和可读性,但使差异更明显 – charlietfl 2015-01-20 22:08:35

+0

感谢,我已编辑它 – ChatCloud 2015-01-20 22:25:12

+1

我建议一个url返回json,另一个返回html,或者需要设置'dataType'以匹配发送的内容。抛出什么错误? – charlietfl 2015-01-20 22:37:03

回答

0

没有足够的信誉评论因此增加一个答案 这里正在扩大使用的数据类型charlietfl评论。

我做了它的工作使用的dataType = “JSON”。根据jQuery文档,Json必须严格格式化。

jQuery.ajax({ 
    url: 'some_other_url', 
    success: function(some_other_response){ 
     jQuery.ajax({ 
     url: 'request_url', 
     dataType:"json", 
     success: function(response){ 
      alert(response.status); //there is nothing 
      alert(response);   //I see 'thestuff' which is expected in 'html' 
     } 
     }) 
    } 
}); 

request_url应该在所有的变量`respose0`返回类似这样(注意,应使用引号,而不是单引号)

{"status":"s","html":"some stuff"}