我正在通过Jquery使用Ajax请求来更新网页的某些部分。
根据从第一个Ajax请求中检索的数据值,我发送(或不发送)另一个嵌套的Ajax请求以获取更多信息。 这意味着第二个Ajax请求嵌套在第一个Ajax响应处理程序中。
基本上:
// Get some information
$.ajax({type: "POST", url: get_some_info_url})
.done(
function(msg)
{
// Given the info contained in msg, let send another request to get more
if(msg.some_info === true)
{
$.ajax({type: "POST", url: get_some_more_info_url})
.done(
function(msg2)
{
// Do something
});
}
// Display "Hello world!" whatever the msg.some_info value is.
$('div#message-id').html("Hello world!");
});
不同的情景:
如果msg.some_info为TRUE => 没有显示的“Hello world!”
如果msg.some_info为FALSE => 显示的“Hello world!”
更奇怪的是:如果我在包含第二个嵌套Ajax调用的行上的Firefox调试器中设置断点(只是在调试器中踩过$ .ajax调用),并且msg.some_info为TRUE => 显示的“Hello world!”
为什么打破代码会“解决”这个问题?
编辑:问题已解决。看到我的回答。
感谢您的帮助。
答案 0 :(得分:0)
我不确定你应该用===运算符测试真相。
根据你的意见,
// Given the info contained in msg, let send another request to get more
你只想检查一下
msg.some_info
存在。在这种情况下,最好通过编写
进行检查if(msg.some_info) {
/* Your code here*/
}
另一个变体,为什么msg.some_info是真的,但" Hello world"没有显示您的代码没有来自服务器的响应并等待它。因为你写了"只是在调试器中跳过$ .ajax调用)并且msg.some_info是TRUE =>显示" Hello world!""。所以你跨过那个"坏点"并且消息是显示的。这让我觉得在代码本身或者没有回来的服务器响应中存在一些问题。
答案 1 :(得分:0)
这个问题在我身边,很抱歉浪费你的时间。
其实我正在删除“你好世界!”第二个Ajax响应处理中的文本。因此调试运行和正常运行之间的区别。处理ajax响应的顺序在调试和不调试时都不一样,因此误导了我对问题的根本原因。
谢谢大家的帮助。