Getting JSON in nested ajax calls

The http request sent to some 'request_url' returns json response in format {'succes': 1, 'html': 'thestuff'}

so when

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

'thestuff' can be found in response.html as expected. But if this ajax is called inside the 'success' callback of another one ajax request then the response.html is coming empty and 'thestuff' is going to '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' 
        }
     }) 
   }
});

Why does it happen?

Update: 'thestuff' contains some js code with {} I can suppose something can get confused but why it works well with single (not nested) ajax request.


Not enough reputation to comment so adding as an answer Here is expanding on charlietfl comment of using dataType.

I made it work using dataType="json". Json has to be strictly formatted though as per jQuery documentation.

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 should return something like this (note, quotes should be used instead of apostrophes)

{"status":"s","html":"some stuff"}
链接地址: http://www.djcxy.com/p/48174.html

上一篇: 在脚本标记中何时需要CDATA部分?

下一篇: 在嵌套的ajax调用中获取JSON