简体   繁体   中英

Google Maps Object to PHP thorough AJAX request

I'm trying to cache Google Maps geocoding results in my database (mongoDB).

So that users would not reach their 2500 daily geocoding quota.

What I'm trying to do is that when Geocoding response is OK then

cache it to my database by sending those response object and status object to my database

as it is by sending those two objects to PHP through AJAX.

and retrieve the stored data and use it just as the data from Google Maps Geocoding.

But I'm facing a problem that PHP does not recognize those objects and cannot put them in

mongoDB.

I have tried to accept those two objects by

$response = json_decode((object) $_POST['response']);
$status = json_decode((object) $_POST['status']);

but those two become 'null' did not help at all.

Should I make a new JSON which is comprised of all strings and numbers to send through AJAX?

Or is there a way to let PHP recognize JSON with JavaScript Object so that I can put that in MongoDB?

EDIT: I think I'm also facing this problem while trying to post those objects to php

My problem has solved by using JSON2 .

Simply I just send

$.post(url, { response:JSON.stringify(response), status:JSON.stringify(response) });

Let php to recognize those objects as string first and then

json_decode is able to convert those JSON strings to array in PHP.

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