我正在尝试从JavaScript脚本进行AJAX调用,但它不断抛出不支持的媒体类型错误。
以下是我正在尝试的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调用工作正常。我在这里缺少什么?
答案 0 :(得分:0)
问题解决了 - 我需要将服务添加到Web.Config(正如Liam指出的另一个问题所指出的那样)。