partiz
partiz

Reputation: 1216

how to get json form remote server via jQuery?

this is a json file:

myjson.php

mylink([
{
"link": "link1",
"url": "http://www.example1.com"
},
{
"link": "link2",
"url": "http://www.example2.com"
},
{
"link": "link3",
"url": "http://www.example3.com"
}
])

And I try call it.

$.ajax({
    url: "json/physician.php",
    dataType: "text",
    success: function(data) {
        json = $.parseJSON(data)
        alert(json.mylink.link[0]);
    }
});

but it do not work.(do not return any alert.) what's my wrong?

Upvotes: 0

Views: 83

Answers (3)

Dinesh Gupta
Dinesh Gupta

Reputation: 51

$.ajax({
    url: 'https://app.zencoder.com/api/v2/jobs/1234/progress.js?api_key=asdf1234',
    dataType: 'jsonp',
    success: function(data){
    // write your code 

    }
});

Upvotes: 0

Prabhat
Prabhat

Reputation: 37

Why you are creating a complex json array , also your json is not a valid json array,while you are creating your json array you can check the valid json in jsonlint

In simple form here is JSON array in javascript and how to retrieve value var mylink=[ { "link": "link1", "url": "http://www.example1.com" }, { "link": "link2", "url": "http://www.example2.com" }, { "link": "link3", "url": "http://www.example3.com" } ];

after that you can get value from any index of array :-

alert(mylink[0].link);
alert(mylink[0].url);

Upvotes: 0

T J
T J

Reputation: 43156

Your JSON is not valid. Valid format for what you're trying to do will be:

{         //json
 "myLink": { //.mylink
    "link": [   //.link
        {         //[0]
            "link": "link1",
            "url": "http://www.example1.com"
        },
        {
            "link": "link2",
            "url": "http://www.example2.com"
        },
        {
            "link": "link3",
            "url": "http://www.example3.com"
        }
    ]
 }
}

You can validate JSON using tools like jsonlint.com

Not sure why the file is a .php file, you can use getJSON method to retrieve json data which will automatically parse the response for you.

Upvotes: 1

Related Questions