How do I encode JSON in PHP via jQuery Ajax post data?
The result is [object object] . yes, that’s what you’ll get if JS casts an object to a string. Your JSON may be correct; you’re just not examining it properly. Try using console.log() instead. Also, use the dev tools to look at the http response that PHP sends back — you should be able to see exactly what the data looks like.
I agree with Spudley and if you just want to ensure try to console.log or alert data.Amount or data.FirstName and it will give you the value.
5 Answers 5
Code example with JSON.stringify:
text.php
$amount, "firstName" => $firstName, "lastName" => $lastName, "email" => $email ); echo json_encode($data); > ?>
Your object is most likely being passed properly. It’s the way you’re capturing the result that returns [object object] as @Spudley explained. The console doesn’t know how to display the construct but you can display specific attributes using the object.attribute syntax. Use console.log() on the JS side or the code below for a prettified output.
// Indent with tabs // Data is the parameter sent to the success function in the ajax handler JSON.stringify( data , null, '\t');
Also Temporarily remove dataType on the ajax handler if you sense there’s a bug somewhere. Getting the ouput to show on a GET request should do. Change this back to POST for any operation that modifies something like a database delete or alter.
Lastly, modify the header as shown in @GroovyCarrot’s answer. If you’re using Chrome the extra whitespace seems to be a bug: Tab and pre wrapped around JSON output in Chrome
Sending JSON to PHP using ajax
I want to send some data in json format to php and do some operation in php. My problem is i can’t send json data via ajax to my php file.Please help me how can i do that. I have tried this way..
8 Answers 8
Lose the contentType: «application/json; charset=utf-8», . You’re not sending JSON to the server, you’re sending a normal POST query (that happens to contain a JSON string).
That should make what you have work.
Thing is, you don’t need to use JSON.stringify or json_decode here at all. Just do:
@Ayyash: If you were sending JSON, you’d have to read the raw input data (from php://input ). I don’t know anything about MVC.NET, so I can’t answer that.
or maybe its an IIS vs Apache issue? I use the same ajax function in both, but in .NET i just grab Request.Post, in PHP that doesn’t work, neither did php://input for some reason, the only thing that worked was passing query string attributes and using $_REQUEST. that hurts
@Ayyash: What’s wrong with using query strings and $_POST ? That’s how HTML forms are submitted. If you really want to send JSON, you can try $HTTP_RAW_POST_DATA if php://input doesn’t work.
I think I get it, «I THINK», see in MVC if you do not set content-type to json, you won’t be a happy person sending back json objects that map to models, but it also is possible to read it from Request. that isn’t the case in PHP. On the other hand if you do choose to go with JSON contentType, JSON.stringify the whole data object is required in MVC forms, but it wouldn’t pass in my PHP form!
jQuery $.ajax request of dataType json will not retrieve data from PHP script
I’ve been looking all over for the solution but I cannot find anything that works. I am trying to get a bunch of data from the database and then via AJAX autocomplete input fields in a form. To do this I’ve decided to use json, because why not, right? Alternatively I’ve been thinking to just send back a delimited string and then tokenise it, which in hind-sight would’ve been much easier and spared me the headache. Since I’ve decided to use json though, I guess I should stick with it and find out what went wrong! What happens is that when the get_member_function() is executed, an error pops up in an alert dialogue and reads «[object Object]». I’ve tried this also using the GET request, and by setting the contentType to ”application/json; charset=utf-8″. Alas, no dice. Can anyone please suggest what I am doing wrong? Take care, Piotr. My javascript/jQuery function is as follows:
function get_member_info() < var url = "contents/php_scripts/admin_scripts.php"; var "select[ name = member ] option:selected" ).val(); $.ajax( < type: "POST", dataType: "json", url: url, data: < get_member: id >, success: function( response ) < $( "input[ name = type ]:eq( " + response.type + " )" ).attr( "checked", "checked" ); $( "input[ name = name ]" ).val( response.name ); $( "input[ name = fname ]" ).val( response.fname ); $( "input[ name = lname ]" ).val( response.lname ); $( "input[ name = email ]" ).val( response.email ); $( "input[ name = phone ]" ).val( response.phone ); $( "input[ name = website ]" ).val( response.website ); $( "#admin_member_img" ).attr( "src", "images/member_images/" + response.image ); >, error: function( error ) < alert( error ); >> ); >
if( isset( $_POST[ "get_member" ] ) ) < $member_id = $_POST[ "get_member" ]; $query = "select * from members where "; $result = mysql_query( $query ); $row = mysql_fetch_array( $result ); $type = $row[ "type" ]; $name = $row[ "name" ]; $fname = $row[ "fname" ]; $lname = $row[ "lname" ]; $email = $row[ "email" ]; $phone = $row[ "phone" ]; $website = $row[ "website" ]; $image = $row[ "image" ]; $json_arr = array( "type" =>$type, "name" => $name, "fname" => $fname, "lname" => $lname, "email" => $email, "phone" => $phone, "website" => $website, "image" => $image ); echo json_encode( $json_arr ); >
Try alert(JSON.stringify(error)) as it should display more information about the error in the alert dialog
I’d guess that there’s a PHP error before JSON is echoed. Check your fetch function, you are addressing data as $row[‘key’], so you should have used mysql_fetch_assoc instead, I believe. Change ‘$row = mysql_fetch_array( $result );’ to ‘$row = mysql_fetch_assoc( $result );’ and see if it works.