I have seen some other posts in Stackoverflow which were related. Tried that code but it did not work out for me.
actually i have a database with 2 images of an actress in my MYSQL database. I am generating JSON data using PHP and it works fine.
I am trying to parse it with Javascript as shown in this fiddle
var json = [{
"id": "1",
"url": "http:\/\/i.imgur.com\/J8yqh3y.jpg"
}, {
"id": "2",
"url": "http:\/\/i.imgur.com\/WNx9i2c.jpg"
}];
var nazriya = json;
$.each(nazriya, function (index, nazriya_nazim) {
$('#url-list').append('<li>' +
'<h4>' + nazriya_nazim.url + '</h4>' +
'</li>');
});
and it works fine.
But if i try to parse it from my PHP file located in my domain. It does not display anything. It shows blank page.
FIDDLE Link : Parsing JSON data on PHP File
type: "POST",
dataType: 'json',
url: "http://chipap.com/apps/nazriya_nazim/test1.php",
success: function (data) {
alert("1");
//var obj = jQuery.parseJSON(idata);
var json = JSON.parse(data);
$.each(json, function (index, nazriya) {
$('#url-list').append('<li>' +
'<h4>' + nazriya.url + '</h4>' +
'</li>');
});
}
I did not write all these code on my own. Browsed Stack and found solutions. But stuck up at the last step (parsing from a PHP file located in my server).
As said by @DaGLiMiOuX tried it in a separate HTML document.
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
$.ajax({
type: "POST",
dataType: 'jsonp',
url: "http://chipap.com/apps/nazriya_nazim/test1.php",
success: function (data) {
alert("1");
var json = data;
$.each(data, function (index, nazriya) {
$('#url-list').append('<li>' +
'<h4>' + nazriya.url + '</h4>' +
'</li>');
});
},
error: function(jqXHR, status, errorText) {
alert('Status code: ' + jqXHR.status +
'\nStatus text: ' + status + '\nError thrown: ' + errorText);
}
});
</script>
</head>
<body>
<ul id="url-list"></ul>
</body>
Now also it shows the same error.
JSON.parse
. The data that you are gonna recieve into your success function (data
) it will be an object already. – DaGLiMiOuX May 22 '13 at 9:03var json = JSON.parse(data);
. You must set itvar json = data;
, but you already havedata
, so this is not needed. Just call in your$.each(json, function (index, nazriya)
yourdata
variable like this$.each(data, function (index, nazriya)
Try and report, please. This should work. – DaGLiMiOuX May 22 '13 at 9:07