无法使用POST请求将数据发送到服务器

3

i have 2 file.

file one.php:

<?php 
    include 'config/dbconnect.php';
    $query = mysql_query("select * from getgift_logs where status = 1") or die(mysql_error());
    echo("<table border='1'>");
    echo("<tr>");
        echo("<td>User ID</td>");
        echo("<td>Yes</td>");
    echo("</tr>");
    while ($row = mysql_fetch_array($query)) {
        echo("<tr>");
        echo("<td>".$row['user_id']."</td>");
        echo "<td><button class='yes' gift_id = '".$row['id']."'>Yes</button></td>";
        echo("</tr>");
    }
    echo("</table>");
 ?>
 <div id='result'></div>
<script type = "text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
 <script>
    $('.yes').click(function() {
        var trans_id=$(this).attr("gift_id");   
    $.ajax({
    url : "process.php?id="+trans_id,
    type: "GET",  
    success:   function(result) {
         $('#result').html(result);
         },  
        });
       });
    </script>

文件 one.php 发送了一个get请求到文件 process.php。这是文件 process.php 的内容:

<?php 
    include("config/dbconnect.php");
    date_default_timezone_set("UTC");
    $id = $_POST['trans_id'];

    $query = mysql_query("select * from getgift_logs where id = '".$id."'");

    $row = mysql_fetch_array($query);
        $topuplink = $row['topuplink'];
        $trans_id = $row['trans_id'];
        $clientsecret = $row['clientsecret'];
        $accesstoken = $row['accesstoken'];
        $lost_money = $row['lost_money'];
        $status = $row['description'];
        $time = date('Ymdhis',time());
        $sign = md5($money."|".$trans_id."|".$time."|".$clientsecret);
         $ch = curl_init();
                $data = array(
            'topup_money' => $lost_money,
            'reference_trans_id' => $trans_id,
            'description'=>$status,
            'request_time'=> $time,
            'sign'=>$sign
            );

              $postvars = '';
              foreach($data as $key=>$value) {
                $postvars .= $key . "=" . $value . "&";
              }
              $url = $topuplink."".$accesstoken;
              curl_setopt($ch,CURLOPT_URL,$url);
              curl_setopt($ch,CURLOPT_POST, 1);
              curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);
              curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
              $response = curl_exec($ch);
              print_r($response);
              curl_close ($ch); 
?>

process.php 文件中,我发送了一个POST请求,但它没能工作(未将数据发送到服务器)。
我不知道哪里出错了?请帮我!感谢所有人!

1
你发送的是 GET 请求,而不是 POST 请求,或者我理解有误吗?type: "GET" - chris85
1
请通过将curl_exec()的响应存储在变量中并使用exit打印来检查它。例如:print_r($response); exit; - AddWeb Solution Pvt Ltd
print_r($response) 返回 {""data":null""} - mrdragon
@mrdragon:意思是*$postvars$url*也包含所需的值,对吗? - AddWeb Solution Pvt Ltd
同时,尝试将var_dump(curl_error($ch));curl_errno结合使用。 - AddWeb Solution Pvt Ltd
显示剩余10条评论
1个回答

0

试试这个

$postvars = '';
foreach($data as $key=>$value) {
    $postvars .= $key . "=" . $value . "&";
}
rtrim($postvars , '&'); // remove last &

$url = $topuplink."".$accesstoken;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, count($data)); // count of total post fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
print_r($response);
curl_close ($ch); 

你能告诉我响应中显示了什么吗? - Disha V.
print_r($response) 返回 {""data":null""} - mrdragon
方法是正确的,但请检查URL和文件返回给你的内容。 - Disha V.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接