PHP returning JSON to JQUERY AJAX CALL
You can return json in PHP this way:
header('Content-Type: application/json');
echo json_encode(array('foo' => 'bar'));
exit;
Jquery: Ajax call to a php script returning JSON data
The issue is because the promise is executed after the request completes. This means that $('body').html(output)
has already been run before you loop over the returned JSON.
To solve this you need to execute all code which depends on the response within the done()
handler. Try this:
$.ajax({
url:"lista.php",
dataType:"json"
}).done(function(data) {
var output = "<ul>";
$.each(data.Conoscenti, function() {
output += "<li>" + this.Nome + " " + this.Cognome + " è un mio conoscente. È nato il " + this.Nato.Giorno + " " + this.Nato.Mese + " " + this.Nato.Anno + "</li>";
});
output += "</ul>";
$("body").html(output);
});
PHP not correctly returning JSON for an AJAX call
Your implementation appears to be using jQuery, or some mock-up of jQuery. If it is not using jQuery, I will be happy to share more solutions to your problem.
Add dataType
$.ajax({
dataType: 'json',
type: "POST",
url: "../api/login.php",
data: { id: username, password: password },
success: function(response) {
alert( "Data Saved: " + response );
$("#login_username").val("");
$("#login_password").val("");
}
});
The problem is that, by default, jQuery expects a string.
var foo = "bar";
foo[1] === "a"; // true
Documentation: jQuery.ajax
Return JSON data from PHP script using AJAX jQuery
You can give a "json"
argument to $.post
to tell it that the response is JSON, and it will automatically parse the response to a Javascript object or array. In my sample code below I'm assuming it's an array, and each element is an object that contains a property that you want to show in the result; replace .someProperty
with the actual property.
$("#computerForm").submit(function(e) {
e.preventDefault();
$.post(this.action, $(this).serialize(), function(info) {
var html = "";
$.each(info, function() {
html += this.someProperty + "<br>";
});
$("#result").html(html);
}, "json");
});
process.php
can use $_POST['serialnumber']
when it's calling the Airtable API.
$sn = $_POST['serialnumber'];
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.
Related Topics
Error Message Strict Standards: Non-Static Method Should Not Be Called Statically in PHP
How to Remove Both .PHP and .Html Extensions from Url Using Nginx
PHP MySQLi Prevent SQL Injection
Is There a PHP Library For Email Address Validation
How to Overwrite a Function in PHP
Incorrect Integer (2147483647) Is Inserted into MySQL
Filter Multidimensional Array Based on Partial Match of Search Value
How to Only Allow Certain Filetypes on Upload in PHP
What Is the Best Collation to Use For MySQL With PHP
Manipulate a String That Is 30 Million Characters Long
PHP Class Instantiation. to Use or Not to Use the Parentheses
How to Fix Trying to Access Array Offset on Value of Type Null Error
In a PHP/Apache/Linux Context, Why Exactly Is Chmod 777 Dangerous