如何调试JSONP AJAX事件


                    

我正在尝试通过JSONP AJAX请求,但是在弄清为什么它不起作用时遇到了问题。 现在我有这个电话</ p>

  $。getJSON(server_path +“ formproxy.php?” + $(form).serialize()+“&action =” + form.action +“&callback =?”,function(data,status,xhr){
     警报(数据);
});
</ code> </ pre>

但是当我运行脚本时,似乎什么都没有发生。 我能想到的最好的办法是jsonp请求遇到错误,但是由于它是jsonp请求,因此实际上没有报告错误,这极大地阻碍了调试。</ p>

我玩过</ p>

  $。ajaxSetup({
     “错误”:function(){
         alert(“错误”);
     }
});
</ code> </ pre>

和</ p>

  $(document).ajaxError(function(e,xhr,settings,exception)){
     警报('失败');
});
</ code> </ pre>

但是我无法触发它们。</ p>

我已将目标网址输出到控制台,即</ p>

  server_path +“ formproxy.php?” + $(form).serialize()+“&action =” + form.action +“&callback =?”
</ code> </ pre>

我可以访问输出的url,看它是否正常工作,并输出?({“ result”:“ success”})</ code>对我来说似乎正确。</ p>

看一下Chrome中的控制台,它甚至没有显示XHR,但我至少可以确定由于断点而达到了$ .getJSON()代码。</ p>

我最后应该指出,我什至不关心从AJAX调用中取回数据。 我只需要跨域发送表单数据,然后让formproxy.php脚本对其进行处理即可。</ p>

任何关于为什么这种方法可能行不通的想法或帮助我了解内部发生的事情的技术都会受到赞赏。</ p>
     </ div>

展开原文

原文

I'm trying to get a JSONP AJAX request to go through, but I'm having problems figuring out why it's not working. Right now I have this call

$.getJSON(server_path+"formproxy.php?"+$(form).serialize()+"&action="+form.action+"&callback=?", function(data, status, xhr) {
    alert(data);
});

but when I run the script nothing seems to happen. The best I can figure is that the jsonp request is running into an error, but because it's a jsonp request it's not actually reporting the error, which is greatly hindering my debugging.

I've played around with

$.ajaxSetup({
    "error":function() {
        alert("error");
    }
});

and

$(document).ajaxError(function(e, xhr, settings, exception) {
    alert('failed');
});

but I can't get either of them to trigger.

I've output the target url to the console, namely

server_path+"formproxy.php?"+$(form).serialize()+"&action="+form.action+"&callback=?"

I can visit the outputted url and see that it is working and outputs ?({"result":"success"}) which seems right to me.

Looking at the console in Chrome, it doesn't even show the XHR, but I'm at least sure the $.getJSON() code is being reached thanks to breakpoints.

I should finally note that I'm not even concerned with getting data back from the AJAX call. I just need to send form data cross domain and let the formproxy.php script process it.

Any thoughts on why this may not be working or techniques to help me see what's happening inside would be much appreciated.

1个回答


您是否尝试过查看Chrome开发者工具中的网络面板 ? 它将显示您在页面上进行的所有http请求/响应。 这样,您就可以确定告诉getJSON调用是否正在向服务器发出请求,以及服务器的响应是什么(如果有)。</ p>

您也可以在Firebug中使用网络面板。</ p>
     </ div>

展开原文

原文

Have you tried looking at the Network Panel in Chrome developer tools? It will show you all the http requests/responses that are being made on your page. That way you can tell for sure whether the the getJSON call is making a request to the server, and what the server response is (if any).

You can also use the Net panel in Firebug for this.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐