user1365420
user1365420

Reputation: 61

How to get response from url in phonegap using javascript,Jquery

I struck with this problem since 2 days, I need to get data from below url using javascript.

http://www.apexweb.co.in/Apex_Quote/Download_Data.asp?un=9393910169

I parsed the static data, but i need to parse data from the url. Help me

var employees = [{"ContactName":"AksharmaDelhi","ContactNumber":"9313146690","MainCategory":"NONLOCAL","SubCategory":"LIG"},
{"ContactName":"AbhijeetPune","ContactNumber":"9271263359","MainCategory":"NONLOCAL","SubCategory":"LIG"},
{"ContactName":"Abhinandan","ContactNumber":"9954534007","MainCategory":"NONLOCAL","SubCategory":"LIG"},
{"ContactName":"Abhishek","ContactNumber":"9302172932","MainCategory":"NONLOCAL","SubCategory":"LIG"},
{"ContactName":"AbhishekPuri","ContactNumber":"9303928233","MainCategory":"NONLOCAL","SubCategory":"LIG"},
{"ContactName":"AbhishekZhakkas","ContactNumber":"9863027167","MainCategory":"NONLOCAL","SubCategory":"MIG"},
{"ContactName":"Abin","ContactNumber":"8891886340","MainCategory":"NONLOCAL","SubCategory":"MIG"},
{"ContactName":"AccountantSrinivas","ContactNumber":"9949861074","MainCategory":"LOCAL","SubCategory":"MIG"},];

alert(employees.length);   //get length 

for(var i = 0;i<employees.length;i++){

alert(employees[i].ContactName + employees[i].ContactNumber + employees[i].MainCategory + employees[i].SubCategory);

}
</script>

Upvotes: 0

Views: 1076

Answers (2)

caiocpricci2
caiocpricci2

Reputation: 7798

There are many ways of doing that, I use $.ajax the following way:

$.ajax({
            url : "http://www.apexweb.co.in/Apex_Quote/Download_Data.asp?un=9393910169",
            type : "get",
            dataType : 'text',
            success : function(data) {                                  
                var employees = $.parseJSON(data);
                for(var i = 0;i<employees.length;i++){
                alert(employees[i].ContactName + employees[i].ContactNumber + employees[i].MainCategory + employees[i].SubCategory);
                }                       
            },
            error : function() {
                //console.log("your call failed");
                }                                   
        });

The data fetched from your URL will be passed as the data parameter in the success callback. Opposed to $.getJson this gives me more control on what I get back.

In this approach I set the dataType to text. Meaning jquery won't try to do anything "smart" with my results. If i had set it to json the data parameter would be parsed and ready to use. You wouldn't need to parse it manually:

var employees = $.parseJSON(data);

Upvotes: 1

Jeff Lee
Jeff Lee

Reputation: 781

For cross Domain ajax, I suggest using $.getJSON()

$.getJSON("http://my-site-return-json.com/index.php?", function(data){
  // Load Finish
});

In the server-side

remember to return the callback

$callback = $_GET['callback'];
$arr = array("a" => "1", "b" => "2");
echo $callback."(".json_encode($arr).")";

Upvotes: 0

Related Questions