Search code examples
javascriptcordovaxmlhttprequestphonegap-desktop-app

Phonegap xmlhttprequest doesn't return xml data


I'm building a hybrid app using phonegap. I'm designing the login page. I've been able to reach the webservice but not able to retrieve the details.

Following is my Script in loginpage for sending http request.

<script>
    $(document).ready(function() {
        $("#btnLogin").click(function() {
            $.ajax({    
                url: "http://developer.meritmerge.com/mmapk/mmservice.asmx/MMLogin",
                type: "GET",
                dataType: "html",
                data: { MailID:$("#username").val(),PWD:$("#password").val()},
                success: function(data)
                {
                    alert("yes");
                },
                error: function(err)
                {
                    alert("Oops! Something went wrong!");
                }
            })
        });
    });
</script>

Following is the output I get when I run this on my browser directly using the url and passing username and password:

<UserDetails xmlns:xsd="http://www.w3.org/2001/XMLSchema" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://meritmerge.com/">
<UserID>357</UserID>
<IsAdmin>true</IsAdmin>
<ResponseCode>1</ResponseCode>
<SchoolID>20</SchoolID>
<RoleID>37</RoleID>
<ImageUrl>http://www.meritmerge.com/ClientsLogo/20.png</ImageUrl>
</UserDetails>

In my script, under succeess part when I give "alert("yes");" it alerts as yes, but when I try to retrieve the actual data i.e., UserID, RoleID, it shows blank data.

Is my dataType correct? It had same reaction when I used dataType as text.

Please help me out. Thanks in advance.


Solution

  • Solved it! It was returning an object, had to just access the text() of it.

    <script>
            $(document).ready(function() {
                $("#btnLogin").click(function() {
                    $.ajax({    
                        url: "http://developer.meritmerge.com/mmapk/mmservice.asmx/MMLogin",
                        type: "GET",
                        dataType: "text",
                        data: { MailID:$("#username").val(),PWD:$("#password").val()},
                        success: function(data)
                        {
                            $response = $(data).find( "ResponseCode" ).text();
                            if($response == 1)
                                {
                                    localStorage["userid"] = $(data).find( "UserID" ).text();
                                    localStorage["isadmin"] = $(data).find( "IsAdmin" ).text();
                                    localStorage["schoolid"] = $(data).find( "SchoolID" ).text();
                                    localStorage["schoolname"] = $(data).find( "SchoolName" ).text();
                                    localStorage["username"] = $(data).find( "UserName" ).text();
                                    localStorage["roleid"] = $(data).find( "RoleID" ).text();
                                    localStorage["mailid"] = $(data).find( "MailID" ).text();
                                    localStorage["imageurl"] = $(data).find( "ImageUrl" ).text();
                                    localStorage["schoolid"] = $(data).find( "StudentID" ).text();
                                    alert("Welcome, "+localStorage["schoolname"]);
                                    window.location.href = "dashboard.html";
                                }
                            else if($response == -999)
                                {
                                    alert("Incorrect Credentials!");
                                    $('input[type="text"]#username').val('');
                                    $('input[type="password"]#password').val('');
                                    $('input[type="text"]#username').focus();
                                }
                        },
                        error: function(err)
                        {
                            alert("Oops! Something went wrong!");
                        }
                    })
                });
            });
        </script>
    

    This is the code I've been using to fetch the data!

    But now it runs on the browser, but on android phone, it always goes to the err part and alerting "Oops! something is wrong!". I'm not sure what's going on!!!!