Javascript 在嵌套ajax调用中获取JSON

Javascript 在嵌套ajax调用中获取JSON,javascript,jquery,ajax,json,Javascript,Jquery,Ajax,Json,发送到某个“request_url”的http请求返回格式为{'succes':1,'html':'thestaff'}的json响应 那么什么时候 jQuery.ajax({ url: 'request_url', success: function(response){ alert(response.html); //'thestuff' is here as expected } }); 如预期,可以在response.html中找到“thestaff”。但

发送到某个“request_url”的http请求返回格式为{'succes':1,'html':'thestaff'}的json响应

那么什么时候

jQuery.ajax({
   url: 'request_url',
   success: function(response){
      alert(response.html); //'thestuff' is here as expected
   }
});
如预期,可以在response.html中找到“thestaff”。但是如果在另一个ajax请求的“success”回调中调用此ajax,那么response.html将变为空,“thestaff”将变为“response”

 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请求。

没有足够的信誉来评论,所以添加作为答案 下面是对使用数据类型的charlietfl注释的扩展

我使用dataType=“json”实现了它。Json必须严格按照jQuery文档进行格式化

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' 
         }
      }) 
    }
});
请求url应该返回如下内容(注意,应该使用引号而不是撇号)


所有变量
respose0
vs
response
vs
respose
中存在大量的打字错误。很难知道什么是什么。建议您在调试和可读性时在每个成功处理程序中对它们进行不同的命名,但使差异更加明显。Hanks,我已经编辑了itI,建议一个url返回json,另一个返回html,或者您需要设置
数据类型
,以匹配发送的内容。抛出了什么错误?这可能是因为您使用的是异步调用,并且当您试图在成功时执行另一个ajax调用时,第一个调用尚未完成,因此它会启动第二个调用(即使在成功回调中),类似的事情以前发生在我身上
{"status":"s","html":"some stuff"}