我尝试使用Jquery调用WCF Web服务。
我尝试使用POSTMAN工具先测试它,但我收到以下错误:
415无法处理邮件,因为内容类型 '多部分/格式的数据; 边界= -------------------------- 786732504131267602580545'不是 期望的类型' text / xml;字符集= UTF-8'
这是我的WCF Web服务代码:
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
int GetAnswers(int queNumber, string answer, string studentID);
这是我用于Jquery的代码:
var form = new FormData();
form.append("queNumber", "1");
form.append("answer", "abcpostman");
form.append("studentID", "11111");
var settings = {
"async": true,
"crossDomain": true,
"url": "http://bschoolapp.azurewebsites.net/Service1.svc",
"method": "POST",
"headers": {
"soapAction": "http://tempuri.org/IService1/GetAnswers",
"Cache-Control": "no-cache",
"Postman-Token": "120e3284-131e-478b-a766-48e8b8688323"
},
"processData": false,
"contentType": false,
"mimeType": "multipart/form-data",
"data": form
}
$.ajax(settings).done(function (response) {
console.log(response);
});
原因是什么?
我将代码更新为以下
var settings = {
"async": true,
"crossDomain": true,
"url": "http://bschoolapp.azurewebsites.net/Service1.svc",
"method": "POST",
"headers": {
"soapAction": "http://tempuri.org/IService1/GetAnswers",
"Content-Type": "text/xml",
"Cache-Control": "no-cache",
"Postman-Token": "7bddc01d-963d-495b-ba4e-c6cb53729fe3"
},
"data": "{\"queNumber\":\"5\",\"answer\":\"sample answer\",\"studentID\":\"11111\"}"
}
$.ajax(settings).done(function (response) {
console.log(response);
});
我收到400 Bad Request