我有一个restful服务,可以与邮递员测试和工作。但是当我使用JQuery $.post时,它就失败了。服务器记录传入的请求,这与从Postman请求记录的请求非常不同。
邮递员请求记录如下(一切正常):26/7月/2015:11:58:27 -0500 "POST /api/customers/1001/users /1.1“200 1
JQuery请求被记录如下(失败):26/Jul/2015:12:03:19-0500 "POST /customer/%5Bobject%20 5Bobject%5D HTTP/1.1“404 1045
Javascript代码。我尝试过许多URL的排列,它似乎不重要我使用什么,它似乎完全忽略它:
var url = 'http://mywebsite.ca/api/customers/1001/users';
$.post({
url: url,
type: 'POST',
dataType: 'text',
data: xml
}).done(function() {
alert('success!');
}).error(function() {
alert('error occurred');
});
我尝试过多种格式的文章,比如使用“dataType”,使用xml的,等等。总是得到相同的服务器错误,URL都搞砸了。
XML looks like this:
var xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
'<ns2:ca.zzz.bus.schemas.Api.xsd xmlns:ns2="uri">' +
' <email>newcustomer@newcustomer.com</email>' +
' <customerId>1001</customerId>' +
' <password>newuserpass</password>' +
'</ns2:ca.avox.bus.schemas.avoxApi.xsd>';
有什么想法吗?
发布于 2015-07-26 10:20:17
没有来自jquery文档的帮助,但是其他人在这里有一个可行的解决方案。
jQuery.ajax({
headers: {
'Accept': 'text/plain',
'Content-Type': 'application/xml'
},
'type': 'POST',
'url': url,
'data': xml,
'content': xml,
'dataType': 'xml',
'success': alert(1),
'error': alert(2),
});
另一个问题(修复)是,当我期望xml时,服务器正在返回文本。是的,我更喜欢JSON,但是有些人(服务器人员)是按他们的方式设置的。
https://stackoverflow.com/questions/31639756
复制相似问题