在嵌套的 ajax 调用中获取 JSON
Getting JSON in nested ajax calls
发送到某个'request_url'的http请求以格式{'succes': 1, 'html': 'thestuff'}返回json响应
所以当
jQuery.ajax({
url: 'request_url',
success: function(response){
alert(response.html); //'thestuff' is here as expected
}
});
正如预期的那样.html可以在响应中找到"thestuff"。但是,如果这个 ajax 是在另一个 ajax 请求的"成功"回调中调用的,那么响应.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请求一起使用。
没有足够的声誉来评论,所以添加为答案这里扩展了使用数据类型的查理特夫尔评论。
我使用 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'
}
})
}
});
request_url应该返回这样的东西(注意,应该使用引号而不是撇号)
{"status":"s","html":"some stuff"}
相关文章:
- 如何使用 Node JS 获取 JSON 嵌套数组值
- 使用Javascript/JQuery获取JSON GET数据
- 在jQuery中获取JSON
- 如何使用jQuery按键搜索和获取json行数据
- 如何使用jquery获取Json的节点
- 通过字符串获取JSON对象
- 通过Azure存储以HTML形式获取JSON文件
- 获取JSON并使用Javascript解析为字符串
- 如何在angular中获取json对象的名称
- 如何使用angular获取json对象的名称
- 如何使用Ajax JQuery.ech()获取JSON值
- 可以't通过从googlemapapi获取json值
- 获取json结果的身份验证问题
- Javascript:获取 JSON 中的值计数
- 按服务获取JSON数据
- 如何使用javascript获取json元素
- 从javascript文件中获取JSON,并使用NodeJS在路由文件中显示
- 如何从给定的代码中获取JSON对象
- 获取 JSON 数据将数组项添加到变量
- 如何从具有动态可观察属性的淘汰对象中获取 JSON 字符串