jqueryajaxpost失败
我无法使用jquery的ajax功能成功发布 运行页面的URL为jqueryajaxpost失败,jquery,ajax,post,Jquery,Ajax,Post,我无法使用jquery的ajax功能成功发布 运行页面的URL为http://localhost:9999,目标(web服务)的URL为http://localhost:8080。不,端口不一样,它们分别是9999和8080 下面是请求和jquery ajax代码 请求: OPTIONS /profile/set_member HTTP/1.1 Host: localhost:8080 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en
http://localhost:9999
,目标(web服务)的URL为http://localhost:8080
。不,端口不一样,它们分别是9999和8080
下面是请求和jquery ajax代码
请求:
OPTIONS /profile/set_member HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Origin: http://localhost:9999
Access-Control-Request-Method: POST
$.ajax({
type: "POST", url: "http://localhost:8080/profile/set_member",
contentType: "application/json", data: member,
error: function(){ alert('Update failed!'); },
processData: false,
success: function(){ alert('Update successful!'); }
});
jQuery ajax代码:
OPTIONS /profile/set_member HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Origin: http://localhost:9999
Access-Control-Request-Method: POST
$.ajax({
type: "POST", url: "http://localhost:8080/profile/set_member",
contentType: "application/json", data: member,
error: function(){ alert('Update failed!'); },
processData: false,
success: function(){ alert('Update successful!'); }
});
调用什么处理程序?
成功
或错误
处理程序?你能详细说明“不起作用”是什么意思吗
您可能希望警报
在错误
处理程序中包含更多信息,如下所示:
error: function(XMLHttpRequest, textStatus, errorThrown) {
//console.log is better at least for debugging. You can change this back to alert
//when your code goes into production
console.log("Update unsuccessful. Status: ", textStatus, " error thrown: ", errorThrown);
}
textStatus
应该能让您了解问题所在。可能的值有“超时”、“错误”、“未修改”和“解析器错误”。一旦找出实际错误,请更新问题
此外,如果您有Firebug,请检查Net选项卡以查看请求和响应。一些常见的错误来源:
- 违反同一原产地政策。您不能将AJAX创建为与父页面不同的URL
- 服务器端错误。如果您的服务器返回500响应代码,请求将失败
- 分析错误。如果您需要一种特定的响应格式,如
或json
,并且响应不是该格式,那么AJAX请求将不成功xml
- 这是跨域ajax调用的问题。基本上(至少在Firefox中),出于安全原因,POST请求被转换为OPTIONS请求。我昨晚遇到了同样的事情,贴在这里
我在localhost:23485上调用了$.ajax,调用了IIS中托管的web服务。因为它们是不同的域,跨域启动并使事情变得困难。在调用ajax的同一域上创建代理,例如在PHP中:
<?php /* get.php */
$url = $_GET["Url"];
echo file_get_contents($url);
?>
这是一个解决方法。您是从同一个域发布的吗?正在运行的页面的URL是什么?一个更令人不安的症状是,$.ajax调用在进行此类调用时通常会报告成功,即使它实际上不起作用。另外,查看您的请求头,我看到源地址和主机地址都是localhost,但来自不同的端口。和我昨晚遇到的完全一样:(.@Dusda:你说得对,这是同一个问题。似乎唯一的解决方案是在ajax调用和web服务之间使用代理。@Mads610,不一定。你可以利用JSONP规范:。我以前已经做过很多次了。Chris是对的;你可以利用JSONP进行跨域调用。只需要多做一点工作.