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>
- Open you developer tool in your browser hit F12 (In Chrome, Firefox or Edge):
- Go to Console tab and find the results.
- Expand the results tell you get to the object you need.
- Right click and `copy property path'.
- 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
PHP Warning: MySQLi_Close() Expects Parameter 1 to Be MySQLi
Converting an Array from One to Multi-Dimensional Based on Parent Id Values
Sort Array in PHP by Value and Maintain Index Association
Format Text in Excel File via PHP
Codeigniter Sessions Not Working After Migration
How to Count Same Values in an Array and Store It to a Variable
Fatal Error: Call to Undefined Method MySQLi_Result::Fetch_All()
PHP Foreach by Reference Causes Weird Glitch When Going Through Array of Objects
Using PHP as a Template Engine
Cannot Send Session Cookie - Headers Already Sent
Vim Inoremap for Specific Filetypes
MySQL Get a Random Value Between Two Values
Laravel Compact() and ->With()
Access Joomla 2.5 from External Script to Get Article by Id
How to Parse a CSV File to Grab the Column Names First Then the Rows That Relate to It