遇到麻烦我认为是一个相对简单的jQuery插件...
该插件应该通过ajax从php脚本中获取数据,以便为a添加选项<select>.ajax请求非常通用:
$.ajax({
url: o.url,
type: 'post',
contentType: "application/x-www-form-urlencoded",
data: '{"method":"getStates", "program":"EXPLORE"}',
success: function (data, status) {
console.log("Success!!");
console.log(data);
console.log(status);
},
error: function (xhr, desc, err) {
console.log(xhr);
console.log("Desc: " + desc + "\nErr:" + err);
}
});
Run Code Online (Sandbox Code Playgroud)
这似乎在Safari中运行良好.在Firefox 3.5中,REQUEST_TYPE服务器上的"OPTIONS"始终为"OPTIONS",并且不会显示$ _POST数据.Apache将请求记录为"OPTIONS"类型:
::1 - - [08/Jul/2009:11:43:27 -0500] "OPTIONS sitecodes.php HTTP/1.1" 200 46
Run Code Online (Sandbox Code Playgroud)
为什么这个ajax调用在Safari中工作,而不是Firefox,以及如何为Firefox修复它?
Response Headers Date: Wed, 08 Jul 2009 21:22:17 GMT Server:Apache/2.0.59 (Unix) PHP/5.2.6 DAV/2 X-Powered-By: PHP/5.2.6 Content-Length 46 Keep-Alive timeout=15, max=100 Connection Keep-Alive Content-Type text/html …
我在StackOverflow中已经阅读了很多类似的问题,但这些解决方案并不适用于我.
我有WCF REST服务:
[<OperationContract>]
[<WebInvoke(UriTemplate = "PostItem",
RequestFormat= WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, Method = "POST")>]
Run Code Online (Sandbox Code Playgroud)
我可以使用Postman(Chrome扩展程序)使用它.我将数据传递为'raw',而不是'urlencoded'.我得到200返回代码.
我需要使用angularjs调用此方法:
$http.post('http://192.168.1.65/Service1.svc/restapi/PostItem',
{
"Address": "?. ??????, ??. ????????-?????????, ?.25",
...
"User": ""
})
Run Code Online (Sandbox Code Playgroud)
我刚从Postman复制了URL和JSON.但我得到错误:
angular.js:10722选项 http://192.168.1.65/Service1.svc/restapi/PostItem http://192.168.1.65/Service1.svc/restapi/PostItem.预检的响应具有无效的HTTP状态代码405
我搜索过类似的问题并找到了两个解决方案:
Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',但它不适用于我的WCF服务在我的Web.Config中设置自定义标头:
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
Run Code Online (Sandbox Code Playgroud)它对我没有帮助.而且我不确定服务器端出错的原因.Postman扩展可以成功调用此方法.
如何使用AngularJS进行相同的POST调用?
更新:
这是OPTIONS请求:
"审核和响应"选项卡为空
更新2:
一切都在IE中正常工作,但在Chrome中不起作用.