简体   繁体   English

ASP AJAX POST调用返回不支持的媒体类型

[英]ASP AJAX POST call returns Unsupported Media Type

I am trying to make an AJAX call from a JavaScript script, but it keeps throwing Unsupported Media Type errors. 我正在尝试从JavaScript脚本进行AJAX调用,但它不断抛出不支持的媒体类型错误。

Here is the code for the AJAX call I am attempting: 这是我尝试的AJAX调用的代码:

var ParamList =
[{
    "username": Username,
    "oldPassword": OldPassword,
    "newPassword": NewPassword
}];

$.ajax({
    type: "POST",
    url: "/LoginService.svc/ChangePassword",
    data: JSON.stringify(ParamList),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        var ReturnCode = data;
        switch (ReturnCode) {
            case 0:
                 $(Message).html("Password changed successfully");
                break;
            case -1:
                 $(Message).html("Password not changed");
                break;
            default:
                 $(Message).html("Error attempting to change the password - return code " + ReturnCode.toString());
                break;
        }
    },
    error: function (HelpRequest, ErrorCode, TheError) {
        $(Message).html("Error attempting to change the password:<br />" + TheError);
    }
});

and here is the code from LoginService.svc: 这是LoginService.svc中的代码:

public class ChangePasswordParamList
{
    public String username;
    public String oldPassword;
    public String newPassword;
}

[ServiceContract]
public interface LoginServiceInterface
{
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
    int ChangePassword(ChangePasswordParamList[] paramList);
}

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

public class LoginService : LoginServiceInterface
{
    public int ChangePassword(ChangePasswordParamList[] paramList)
    {
        String username = paramList[0].username;
        String oldPassword = paramList[0].oldPassword;
        String newPassword = paramList[0],newPassword;

        int ReturnVal = 0;
        //  do some stuff here
        return ReturnVal;
    }
}

A similar AJAX POST call elsewhere in the code works fine. 在代码的其他地方进行类似的AJAX POST调用也可以正常工作。 What am I missing here? 我在这里想念什么?

问题已解决-我需要将服务添加到Web.Config(正如Liam指出的另一个问题中指出的那样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 POST请求返回415-不支持的媒体类型 - POST request returns 415 - Unsupported Media Type POST 得到 415(不支持的媒体类型)而 POSTMAN 返回 200 - POST getting 415 (Unsupported Media Type) while POSTMAN returns 200 在ajax调用上获取“不受支持的媒体类型415”-Jquery,ajax - Getting an “Unsupported media type-415” on an ajax call- Jquery,ajax 415不支持的媒体类型jQuery Ajax - 415 Unsupported Media Type jQuery Ajax Java POST 415(不支持的媒体类型) - java POST 415 (Unsupported Media Type) 415(不支持的媒体类型)与REST Post请求 - 415 (Unsupported Media Type) with REST Post request Java POST请求不支持的媒体类型 - Unsupported Media Type with Java POST request AJAX POST引发415不支持的媒体类型错误,并显示消息“请求必须具有“ Content-Type:application / vnd.api + json”” - AJAX POST throws a 415 Unsupported Media Type Error with message 'Request must have “Content-Type:application/vnd.api+json”' 服务器响应的Ajax / JSON状态为415(不支持的媒体类型) - Ajax/JSON the server responded with a status of 415 (Unsupported Media Type) 尝试将formData发布到Spring时的415(不受支持的媒体类型) - 415 (Unsupported Media type) when trying to post formData to Spring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM