[英]JQuery data is not passed in the ajax post request, Hence get 415 Unsupported Media Type status code
I am trying to make a call to an api (post) via jquery, but the jquery call does not pass any data to the api and hence it fails by sending a response with status code 415 Unsupported Media Type. 我正在尝试通过jquery调用api(发布),但是jquery调用未将任何数据传递给api,因此通过发送状态码为415 Unsupported Media Type的响应而失败。 I have pasted the jquery code below.
我已经粘贴了下面的jQuery代码。 Am I missing something?
我想念什么吗?
login: function () { 登录名:function(){
var authData = {
"UserName": $("#email").val(),
"Password": $("#pass").val()
};
var url = apiUrl() + '/sessiontoken';
$.ajax(url, {
type: "post",
data: JSON.stringify(authData),
success: function (data, textStatus, jqXHR) {},
beforeSend: function (xhr) {
xhr.setRequestHeader("accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
}
});
return false;
}
The url was returning 404 status from rest console. 网址从其他控制台返回404状态。 Corrected the url and everthing is working fine now.
更正了网址,一切正常。
Your ajax settings is wrong because you have opened {
at wrong place. 您的ajax设置错误,因为您在错误的位置打开了
{
。 It should be like below. 它应该像下面这样。 Also, since you are posting to another domain (cross domain request), set
crossDomain: true
in ajax settings: 另外,由于要发布到另一个域(跨域请求),
crossDomain: true
在ajax设置中将crossDomain: true
设置为:
$.ajax({
url: url,
type: "post",
crossDomain: true,
data: JSON.stringify(authData),
success: function (data, textStatus, jqXHR) {},
beforeSend: function (xhr) {
xhr.setRequestHeader("accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.