How can I send JSON data to server

前端 未结 7 988
旧巷少年郎
旧巷少年郎 2021-01-04 00:25

Well, here is the story:

I have some data need to send to server, but they should turned into JSON dataType first.

I made such ajax call:

            


        
相关标签:
7条回答
  • 2021-01-04 00:56
    dataType: 'json',
    
    0 讨论(0)
  • 2021-01-04 01:01

    Try this: http://www.abeautifulsite.net/blog/2008/05/postjson-for-jquery/

    Its a lot shorter:

    $.post(url, data, function(response) {
        // Do something with the response
    }, 'json');
    
    0 讨论(0)
  • 2021-01-04 01:02
     var data = {'bob':'foo','paul':'dog'};
     $.ajax({
       url: url,
       type: 'POST',
       contentType:'application/json',
       data: JSON.stringify(data),
       dataType:'json'
     });
    

    /** Added **/

    The above does not do anything with the response from the server if you need to do something then a callback will be called when the server has responded.

     var data = {'bob':'foo','paul':'dog'};
     $.ajax({
       url: url,
       type: 'POST',
       contentType:'application/json',
       data: JSON.stringify(data),
       dataType:'json',
       success: function(data){
         //On ajax success do this
         alert(data);
          },
       error: function(xhr, ajaxOptions, thrownError) {
          //On error do this
            if (xhr.status == 200) {
    
                alert(ajaxOptions);
            }
            else {
                alert(xhr.status);
                alert(thrownError);
            }
        }
     });
    
    0 讨论(0)
  • 2021-01-04 01:04

    There are many ways to send JSON data to the server

    1. Using Ajax

    var data = <?php echo json_encode($data) ?>;
    var url  = '<?php echo $url ?>';
    jQuery.ajax({
        type: "POST",
        url: url,
        data: JSON.stringify(data),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data){
            var jsonObj = jQuery.parseJSON(data);
            alert(jsonObj.encPassword);
        },
        failure: function(errorMsg) {
            alert(errorMsg);
        }
    });
    

    2. Using Curl

    <?php
    $content = json_encode($data);
    
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER,
            array("Content-type: application/json"));
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //curl error SSL certificate problem, verify that the CA cert is OK
    
    $result     = curl_exec($curl);
    $response   = json_decode($result);
    var_dump($response);
    curl_close($curl);
    ?>
    

    3. Using Streams

    <?php
    $options = array(
        'http' => array(
            'method'  => 'POST',
            'content' => json_encode( $data ),
            'header'=>  "Content-Type: application/json\r\n" .
                        "Accept: application/json\r\n"
          )
    );
    
    $context     = stream_context_create($options);
    $result      = file_get_contents($url, false, $context);
    $response    = json_decode($result);
    var_dump($response);
    

    4. Raw HTTP Post

    Using Zend Framework’s HTTP client: http://framework.zend.com/manual/en/zend.http.client.advanced.html#zend.http.client.raw_post_data

    $json = json_encode($data);
    $client = new Zend_Http_Client($url);
    $client->setRawData($json, 'application/json')->request('POST');
    var_dump($client->request()->getBody());
    

    SOURCE:- https://blog.magepsycho.com/sending-json-data-remote-server/

    0 讨论(0)
  • 2021-01-04 01:06

    I agree the data must be converted into a JSON string, not only to agree with the dataType and contentType setup but more importantly, to satisfy the server.

    data: JSON.stringify(data),
    dataType:'json'
    
    0 讨论(0)
  • 2021-01-04 01:15

    Also, is necesary can create a parameter and assign the value with JSON.stringify

    ....
    data: "jsonString="+JSON.stringify(data),
    ...
    
    0 讨论(0)
提交回复
热议问题