AJAX调用Web服务返回XML而不是对象

时间:2014-02-05 16:36:07

标签: c# ajax json

我有一个小型系统的Web服务来处理Ajax处理的登录请求。我已经尝试了几种方法来返回一个带有对象的JSON响应,但它返回为undefined或XML。

C#服务

[WebService(Namespace = "WSCSystem_Helpers")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class LoginService : System.Web.Services.WebService
{

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public UserModel UserLogin(string userName, string password)
    {
        UserModel user = new UserModel();
        user.UserName = userName;
        user.Password = password;
        user = WSCSystem.Helpers.Login.LoginUser(user);

        //string json = JsonSerializer.ToJSON(user);
        //return json;
        return user;
    }
}

的Javascript

function login() {
username = $("#txtUserName").val();
password = $("#txtPassword").val();
$.ajax({
    type: 'POST',
    url: 'Helpers/LoginService.asmx/UserLogin',
    data: { "username": username, "password": password },
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    success: function (data) {

        alert(data.d.UserName);
        //alert("Success");

    },
    error: function (e) {
        alert(e.status);
        alert(e.statusText);
    }
});

}

根据我配置contentType或数据类型的方式,我得到500或200的错误响应,如果我删除它们,我得到一个未定义的对象响应。

如何在可用对象中获得响应?

XML响应(在错误函数中接收)

e.status = 200

<?xml version="1.0" encoding="utf-8"?>
<UserModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="WSCSystem_Helpers">
  <UserName>username</UserName>
  <Password>123456</Password>
  <CustEmployeeRecordID>0</CustEmployeeRecordID>
  <AddressID>0</AddressID>
  <UserExists>false</UserExists>
  <MiddleInit />
  <Suffix />
  <Address2 />
  <Role>CUST</Role>
  <RoleCode>C</RoleCode>
</UserModel>

0 个答案:

没有答案