weixin_33743248 2016-06-24 07:58 采纳率: 0%
浏览 35

从GET切换到POST

I have the following Ajax request:

// JavaScript
function myFunc(pid) {
    $.ajax({
        type : "GET",
        url : "testback.php",
        contentType : "application/json; charset=utf-8",
        dataType : "json",
        data : {
            q : "testrequest",
            pid : pid
        },
        success : function(data) {
            console.log(data)
        },
        error : function(jqXHR, status, error) {
            console.log(status, error);
        }
    });
}

// PHP
require_once ("dbconnect.php");
if (isset ( $_GET ['q'] )) {
    if ($_GET ['q'] == "testrequest") {
        $pid = $_GET ['pid'];

        $query = "SELECT * FROM `tab1` WHERE `pid` = " . $pid;

        $json = array ();
        if ($result = $link->query ( $query )) {
            while ( $row = $result->fetch_assoc () ) {
                array_push ( $json, $row );
            }
        }

        header ( "Content-type: application/json; charset=utf-8" );
        die ( json_encode ( $json ) );
        exit ();
    }
    die ();
}

It sends a request to my MySQL database and returns the expected output.

However, I now want to switch to POST, rather than GET.
When I just swap GET with POST:

// JavaScript
function myFunc(pid) {
    $.ajax({
        type : "POST", // POST
        url : "testback.php",
        contentType : "application/json; charset=utf-8",
        dataType : "json",
        data : {
            q : "testrequest",
            pid : pid
        },
        success : function(data) {
            console.log(data)
        },
        error : function(jqXHR, status, error) {
            console.log(status, error);
        }
    });
}

// PHP
require_once ("dbconnect.php");
if (isset ( $_POST ['q'] )) { // POST
    if ($_POST ['q'] == "testrequest") { // POST
        $pid = $_POST ['pid']; // POST

        $query = "SELECT * FROM `tab1` WHERE `pid` = " . $pid;

        $json = array ();
        if ($result = $link->query ( $query )) {
            while ( $row = $result->fetch_assoc () ) {
                array_push ( $json, $row );
            }
        }

        header ( "Content-type: application/json; charset=utf-8" );
        die ( json_encode ( $json ) );
        exit ();
    }
    die ();
}

I get the following error in the console:

parsererror SyntaxError: Unexpected end of JSON input

The request payload is still q=testrequest&pid=1.

What else do I need to change, in order to switch from GET to POST?

  • 写回答

2条回答 默认 最新

  • ?Briella 2016-06-24 08:18
    关注

    In your Ajax function you need to omit the content type as it is already defined in the Ajax Call. Delete the line "contentType : "application/json; charset=utf-8" shown below:

    $.ajax({
        type : "GET", // Or POST
        url : "testback.php",
        contentType : "application/json; charset=utf-8", // REMOVE THIS LINE!!
        dataType : "json",
        data : {
            q   : "testrequest",
            pid : pid
        },
        success : function(data) {
            console.log(data)
        },
        error : function(jqXHR, status, error) {
            console.log(status, error);
        }
    });
    

    It should work just fine after that! Cheers!

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line