简体   繁体   中英

JSON POST Request - JSON data, How to do a post request with data in JSON format

I want to post following data :

{
  "contactsync":
      { "rev":4,
        "contacts":[
                   { "fields": [
                                {
                                   "value":
                                      {
                                        "givenName":"dfgheeej",
                                        "familyName":"ffftestfff"
                                      },
                                   "type":"name",
                                   "flags":[],
                                   "op":"add"
                                },
                                {
                                   "value":"fffffff",
                                   "type":"nickname",
                                   "flags":[],
                                    "op":"add"
                                },
                                {
                                   "value":"fffff@fffff.com",
                                   "type":"email",
                                   "flags":[],
                                   "op":"add"
                                }
                            ],
                     "categories":[],
                     "op":"add"
                   }
       }
}

to a url. ( like : site.com/anything/add?format=json&wssid=t4764.444&wssid=some_value&windowid=&r=776566756 )

How do i post above data to a url through possibly javascript ? ( i want to create a html file for it containing script to do it. )

This all will not happen on same server, i want to post data from other server to other one.

You can use ajax if you script will be on the same server, the server support CORS or the server support JSONP. Using jQuery it will be:

var data = {"contactsync":{"rev":4,"contacts":[{"fields":[{"value":{"givenName":"dfgheeej","familyName":"ffftestfff"},"type":"name","flags":[],"op":"add"},{"value":"fffffff","type":"nickname","flags":[],"op":"add"},{"value":"fffff@fffff.com","type":"email","flags":[],"op":"add"}],"categories":[],"op":"add"}]}};
var url = 'site.com/anything/addformat=json&wssid=t4764.444&wssid=some_value&windowid=&r=776566756';

$.post(url, JSON.stringify(data), function(response) {

});

if you can't use ajax you will need to create a proxy in php (just pass the json to the remote address and print response) like:

function post($url, $data) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    return curl_exec($ch);
}

function get_raw_post_data() {
    if (isset($GLOBALS['HTTP_RAW_POST_DATA'])) {
        return $GLOBALS['HTTP_RAW_POST_DATA'];
    } else {
        return file_get_contents('php://input');
    }
}

$url = "site.com/anything/addformat=json&wssid=t4764.444&wssid=some_value&windowid=&r=776566756";

echo post($url, get_raw_post_data());

then insidead of url of remote site you use your script on the same server.

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