我正在尝试从 JavaScript 脚本进行 AJAX 调用,但它不断抛出 Unsupported Media Type 错误。
这是我正在尝试的 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);
}
});
这是来自 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;
}
}
代码中其他地方的类似 AJAX POST 调用工作正常。我在这里想念什么?