samedi 9 mai 2015

Calling JSON and data retrieval working in browser but not in Cordova emulator or mobile

I am building a mobile web app using html5, there I am fetching the data from my WCF rest serice. Problem is it's working fine in browser, when converted in to app using Apache Cordova it's not working.

I'm getting http_request.readyState :4 and http_request.status :0 instead of 200. I thought it's a problem with cross domain issue so used ajax instead of Java function and enabled CORS in web.config of WCF still problem persists

Can anyone help out please?

Thanks in advance

Vikram

PS : when using ajax function its getting into error function but displaying a error message success:200, don't what it means

$.ajax({
                dataType: 'jsonp',
                type: "GET",
                url: "http://dev_test_srv/RestServiceImpl.svc/json/123?callback=?",
                success: function (data) {
                    alert(123);
                    alert("working");
                },
                error: function (error) {
                    alert(error.statusText + "-" + error.status);
                }
});

var data_file = "http://dev_test_srv/RestServiceImpl.svc/Employees/" + id + "/" + pwd;
alert(data_file);

var http_request = new XMLHttpRequest();

try {
                // Opera 8.0+, Firefox, Chrome, Safari
                http_request = new XMLHttpRequest();
} catch (e) {
                // Internet Explorer Browsers
                try {
                    http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
                    try {
                        http_request = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch (e) {
                        // Something went wrong
                        alert("Your browser broke!");
                        return false;
                    }
                }
}

http_request.onreadystatechange = function () {
                alert(http_request.readyState + '--' + http_request.status);
                if (http_request.readyState == 4 && http_request.status == 200) {
                    var stringouptut = http_request.responseText;
                    var replacedstring = stringouptut.replace(/\\/g, "");
                    var newresult = "{ \"Data\": " + replacedstring.substring(replacedstring.indexOf("["));
                    var final = newresult.substring(0, newresult.length - 2) + " " + " }";

                    final = JSON.parse(final);

                    if (final.Data.length > 0) {

                        if (final.Data[0].status == "Valid") {

                            localStorage.UserId = id;

                            window.location = "doctors.html";
                        }
                        else {
                            document.getElementById("lblerror").innerHTML = "Invalid Employee ID/Password";
                        }

                    }
                }
            }
            http_request.open("GET", data_file, true);
            http_request.send();

Config settings in web.config :

<httpProtocol>
    <customHeaders> 
         <add name="Access-Control-Allow-Origin" value="*"/>
         <add name="Access-Control-Allow-Headers" value="Content-Type, Accept"/>
         <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS" />                             <add name="Access-Control-Max-Age" value="1728000" />
    </customHeaders> 
</httpProtocol>

Aucun commentaire:

Enregistrer un commentaire