如何检查我是否正在发送 ajax 调用所需的内容?
我正在尝试使用 JQuery 进行 AJAX 调用,实际上我有下一个代码:
$('#form-check').click(function(){
$.ajax({
url: "http://www.domain.com/user/checkurl/",
type: "GET",
data: $(location).attr('pathname').substring(17) + "/" + $('#urlTxt').val(),
error: function(){
$('#urlTxt').css('background','#ce2b06');
},
success: function(data){
$('#urlTxt').css('background','#83aa07');
$('#form-check').css('display','none');
$('#form-submit').css('display','block');
$('#result').append(data);
}
});
});
但是,当我检查 Firebug 时,我没有得到任何我能看到的响应,我只看到正在发送的消息,并且状态显示为(200 - 好的)但没有任何内容被发送回来,并且 Ajax 调用本身执行“成功”子句,只是它不附加数据。我怎样才能检查这个?或者我错过了什么?
I am trying to do an AJAX call using JQuery, I actually have the next code:
$('#form-check').click(function(){
$.ajax({
url: "http://www.domain.com/user/checkurl/",
type: "GET",
data: $(location).attr('pathname').substring(17) + "/" + $('#urlTxt').val(),
error: function(){
$('#urlTxt').css('background','#ce2b06');
},
success: function(data){
$('#urlTxt').css('background','#83aa07');
$('#form-check').css('display','none');
$('#form-submit').css('display','block');
$('#result').append(data);
}
});
});
However when I check with Firebug I don't get any response that I can see, I only see the message being sent and on status it says (200 - ok) but nothing gets sent back, and the Ajax call itself does the "success" clause, except that it doesn't append the data. How can I check this? Or what am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个跨域请求。默认情况下,浏览器会阻止来自跨域站点的响应。您需要使用 jsonp 作为数据类型。只要用 google 搜索一下,你就可以看到如何使用 jquery API 来完成它。堆栈溢出也有围绕这些的问题。
在同源策略下,server1.example.com 提供的网页无法正常连接或与除 server1.example.com 以外的服务器通信。 HTML 元素是一个例外。利用元素的开放策略,某些页面使用它们来检索对来自其他来源的动态生成的 JSON 格式数据进行操作的 Javascript 代码。这种使用模式称为 JSONP。
我也不确定你将参数传递给服务器的方式。理想情况下,你的数据字段应该类似于 json 对象,就像
你理想情况下会提到你期望收到的响应类型,以便 jquery 可以正确处理它。
这是通过设置 dataType 选项
dataType: "json" // 可以是 html、xml 等来完成
This is a cross domain request. Browsers by default block responses from cross domain sites. You need to use jsonp as the datatype. Just google the same and you can see how it can be done using the jquery API. Stack overflow has questions around these too.
Under the same origin policy, a web page served from server1.example.com cannot normally connect to or communicate with a server other than server1.example.com. An exception is the HTML element. Taking advantage of the open policy for elements, some pages use them to retrieve Javascript code that operates on dynamically-generated JSON-formatted data from other origins. This usage pattern is known as JSONP.
also I am not sure about the way you are passing parameters to the server.. your data field should ideally resemble a json object like
also you would ideally would wanna mention the type of response you are expected to receive so that jquery can process it correctly.
This is done by setting the dataType option
dataType: "json" // can be html, xml etc
如果您使用的任何其他浏览器没有任何此类工具,那么您始终可以使用 Fiddler 显示系统上的所有
HTTP
流量。If you are using any other browser which do not have any such tools then you can always use Fiddler which shows all the
HTTP
traffic on the system.是跨域请求吗?
如果是跨域请求,应该尝试设置选项 crossDomain:true
检查 jQuery API http ://api.jquery.com/jQuery.ajax/
is it a cross-domain request?
if it is a cross-domain request, you should try to set the option crossDomain:true
check the jQuery API http://api.jquery.com/jQuery.ajax/
Firebug 的
Net
选项卡和 HTTPFox 都可以让您查看客户端和服务器之间的完整流量,包括所有标头。Firebug's
Net
tab and HTTPFox both let you see the full traffic between client and server, including all the headers.