Jquery $.Ajax Request of Datatype JSON Will Not Retrieve Data from PHP Script

Not getting a response for an AJAX call to PHP (with JSON data)

How to read JSON objects from PHP and display in browser?

There are lots of comments on your code:

  • While you already getting a json return from server; you don't to parse that. Its already a json object.
  • You can set async:true to get a promise data
  • The way you loop through objects you need to do that properly. see image how to get the object path correctly.
  • You can use $ token instead of jQuery token; unless you purposely need that.
  • I am not sure if this is the best approach; but it give the needed result as explained in your question.

The code is bellow tested with some comments:

<script type="text/javascript">
loadcall("test");
// as pointed you need to call the function so it runs

function loadcall(data) {
$.ajax({
async: true,
method: 'POST',
crossDomain: true,
dataType: 'json', //your data type should be JSON not JSONP
url: 'page.php?getcodenode',
data: {
'arg': data
},
success: function(result) {
console.log(result);
// see attached image how to get the path for object
var ret = result;
var el = $('#abc');
for (en in ret.itens) {
console.log(ret.itens[en].ds);
el.append('<div id="item_' + ret.itens[en].id +
'">' + ret.itens[en].lb + ', ' + ret.itens[en].ds + '</div>');
}
},
error: function(result) {
console.log(result);
}
});
}
</script>

How to get object path from console result?

  1. Open you developer tool in your browser hit F12 (In Chrome, Firefox or Edge):
  2. Go to Console tab and find the results.
  3. Expand the results tell you get to the object you need.
  4. Right click and `copy property path'.
  5. Use the object path as needed in your code.

AJAX call with Content Type Application/json not retrieving data from PHP

Finally Solved the problem,
I completely teared down the PHP cUrl function, in a way that only basic functionality was left. Now the JSON data is returned to the AJAX call. What I noticed was that there were some warnings when the cUrl function was executed (like undefined variables) but the JSON data was still returned in the PHP function. So, primary you should think that warnings should be no problem. After resolve the warning coming from the cUrl function, the JSON data as returned properly to the AJAX call. In other words, the Ajax call will not return data when a PHP warning is occurring.

Cannot get data in PHP from JQuery Ajax post with JSON

Change into

var json = {"data":{"Num String":"2","Num":3,"Num":11,"Num":2,"Num":"?"}};


Related Topics



Leave a reply



Submit