简体   繁体   English

jQuery数据未在ajax发布请求中传递,因此获得415不支持的媒体类型状态代码

[英]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.

相关问题 400错误的请求或415不支持的媒体类型在Ajax呼叫中 - 400 Bad request or 415 Unsupported Media type in Ajax post call 415(不支持的媒体类型)与REST Post请求 - 415 (Unsupported Media Type) with REST Post request 415不支持的媒体类型jQuery Ajax - 415 Unsupported Media Type jQuery Ajax 服务器响应的Ajax / JSON状态为415(不支持的媒体类型) - Ajax/JSON the server responded with a status of 415 (Unsupported Media Type) HTTP状态415 - JQUERY中使用JERSEY实现的Restful WS中的AJAX调用不支持的媒体类型 - HTTP Status 415 - Unsupported Media Type for AJAX call in JQUERY to Restful WS implemented with JERSEY 带有jQuery的415(不受支持的媒体类型) - 415 (Unsupported Media Type) with jquery HTTP状态415-将数据从ajax作为json传递到restfull服务器时出现不受支持的媒体类型错误 - HTTP Status 415 - Unsupported Media Type error when passing data from ajax to restfull server as json 415不支持的媒体类型jQuery - 415 Unsupported Media Type jQuery 将请求发送到缺陷跟踪工具以创建问题时,获取“ 415不支持的介质类型”响应状态 - Get '415 Unsupported Media Type' response status when sending request to defect tracking tool for creating issue ajax json发布到Spring mvc控制器“ 415不支持的媒体类型” - ajax json Post to Spring mvc Controller “415 Unsupported Media Type”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM