简体   繁体   English

在javascript Ajax调用中从Web服务中的XMLDocument获得响应…?

[英]get response from XMLDocument in web service in javascript Ajax call…?

i'm calling a .asmx web service from Server is is calling fine, it return response in XMLDocument and i need only result like TRUE or FALSE 我正在从Server调用.asmx Web服务,调用正常,它在XMLDocument中返回响应,我只需要像TRUE或FALSE这样的结果

service .cs file code is : 服务.cs文件的代码是:

 [WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public string UserLogin(string u_name,string user_pwd)
    {
        string result = "true";
        if (u_name == "unicolumn" && user_pwd=="admin")
        {
            result = "true";
        }

    else
        {
            result = "false";

        }
        JavaScriptSerializer js = new JavaScriptSerializer();
        return js.Serialize(result);
    }

my ajax call code is like this: 我的ajax调用代码是这样的:

   function apiLogin()
    {
     var UserName = document.getElementById('txtUserName').value;
   var PWD = document.getElementById('txtUserPWD').value;
$.ajax({
    type: "GET",
        url: "http://192.168.200.56/ChatApp.asmx/UserLogin?u_name="+UserName +"&user_pwd="+PWD,
    datatype:"html/xml",
    success: function (xml) {


    //alert(xml.find("string").text());
    console.log(xml);
},

    failure: function(Response) {
        alert(Response.d);
    }
});

}

and the response of this ajax call is: 这个ajax调用的响应是:

  <string xmlns="http://tempuri.org/">"true"</string>

and i want this is only as TRUE or FALSE , someone can help me to figure out what i'm missing here. 我希望这只是TRUE或FALSE,有人可以帮助我弄清楚我在这里缺少什么。 Thanks in advance. 提前致谢。

You can use Response.Write(). 您可以使用Response.Write()。 In your WebMethod: 在您的WebMethod中:

if (u_name == "unicolumn" && user_pwd=="admin")
        {
            Response.Write("true");
        }

    else
        {
            Response.Write("false");

        }

Then, in the ajax call: 然后,在ajax调用中:

...
     success: function (xml) {
        if (xml.responseText == "false") { 
              (your code) 
       }
},

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM