如何使用php的post提交json数据...

解决方案 »

  1.   

    不知道我有没理解错,希望帮到你
    form表单中的submit改成button
    JS中做以下三步:
    1.中先获取你需要的内容
    2.把数据打包成json格式
    3.form.submit()
      

  2.   

    我也想知道,一般来说都是用form提交字段的吧。
      

  3.   

    以下是一个例子,你可以按需扩展。
    $post_data =   
                array(  
                        'name'=>'aaa',  
                        'age'=>45,  
                );  
          $url='http://www.example.com/example.php';  
      
        $ch = curl_init();  
        curl_setopt($ch, CURLOPT_POST, 1);  
        curl_setopt($ch, CURLOPT_URL,$url);  
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result=curl_exec($ch);  
        echo $result; 
      

  4.   

    a.php
    $data = array("name" => "testname", "age" => "25");
    $data_string = json_encode($data);$ch = curl_init('http://localhost/test/a.php');
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data_string)));
    $data = curl_exec($ch); print_r($data);b.php
    1.echo file_get_contents("php://input");
    2.echo $GLOBALS["HTTP_RAW_POST_DATA"];1或2均可获取到由a.php页面传过来的json数据
      

  5.   

    json就是字符串,大的字符串用post小的用get方式不就可以了。