I am trying to make an AJAX call from a JavaScript script, but it keeps throwing Unsupported Media Type errors.
Here is the code for the AJAX call I am attempting:
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:
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. What am I missing here?
问题已解决-我需要将服务添加到Web.Config(正如Liam指出的另一个问题中指出的那样)。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.