简体   繁体   中英

How to pass a javascript object to a PHP file while using ajax

I have a javascript object which I want to pass to a PHP file while using jQuery's ajax-implementation.

I've tried to directly pass it to it but this doesn't work, because it isn't escaped or anything. I've tried to use JSON.stringify but this isn't working for me either.

Is there a way to 'serialize' a javascript object to a POST-string?

Update, I'm using JSON.stringify() again. The result is:

The result of JSON.stringify() is:

{\"label\":\"Borne, Overijssel, Nederland\",\"value\":\"Borne, Overijssel, Nederland\",\"geocode\":{\"address_components\":[{\"long_name\":\"Borne\",\"short_name\":\"Borne\",\"types\":[\"locality\",\"political\"]},{\"long_name\":\"Borne\",\"short_name\":\"Borne\",\"types\":[\"administrative_area_level_2\",\"political\"]},{\"long_name\":\"Overijssel\",\"short_name\":\"OV\",\"types\":[\"administrative_area_level_1\",\"political\"]},{\"long_name\":\"Nederland\",\"short_name\":\"NL\",\"types\":[\"country\",\"political\"]}],\"formatted_address\":\"Borne, Nederland\",\"geometry\":{\"bounds\":{\"ca\":{\"b\":52.2832527,\"f\":52.3151634},\"ea\":{\"b\":6.688658900000064,\"f\":6.801415300000031}},\"location\":{\"Ya\":52.3002366,\"Za\":6.753725799999984},\"location_type\":\"APPROXIMATE\",\"viewport\":{\"ca\":{\"b\":52.2832527,\"f\":52.3151634},\"ea\":{\"b\":6.688658900000064,\"f\":6.801415300000031}}},\"types\":[\"locality\",\"political\"]}}

When I do a json_decode it results to NULL. Any suggestions?

If your passing an object as a string thats in legitmate JSON format, to PHP try using

json_decode() on the php side of things. Example

<?php
   $ojb = json_decode($_POST['my_json_string']);
?>

What this will do is turn your object into an array or object depending on which version of PHP you are using, and in some cases the object will turn into an array with multiple objects in it.. example:

Array(
    [0] stdClass (
            'key1'=>'val1'
            'key2'=>'val2'
            'key3'=>'val3'
         )
)

which I know the above isnt a good representation, but its a representation in the lines there of.

After that PHP side you can work with the variable $ojb like any other array/object.

$something = $ojb[0]->key1;

EDIT I notice your string now. The fact that the quotes are escaped in the string, breaks the fact that its a JSON object, with that you can do one of two things.. Either just pass the object to PHP through your post/get as is, without running it through strigify or.. you could try on the PHP side, if there is a need to strigfy it..

$ojb = stripslashes($_POST['my_json_string']); $ojb = json_decode($ojb);

which will attempt to remove the slashes from the quotes, before putting it through the decode process.

http://php.net/manual/en/function.json-decode.php

You can specify a raw body of the POST request. The raw data would be the result of a JSON.stringify call which would mean that you should specify an appropriate Content-Type header.

$.ajax(url, {
  type: 'POST',
  contentType: 'application/json',
  data: JSON.stringify(data),
  processData: false // prevent escaping and other processing
});

You can then deserialize the object in PHP like this:

$json = file_get_contents('php://input');
$data = json_decode($json);

The raw request body is mapped onto the special path php://input

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM