简体   繁体   中英

HTTP POST not working (probably some silly mistake unable to determine)

Here is a code to POST JSON data. It should work . But I am not sure where I did the mistake . probably a silly one, but unable to detect

<!DOCTYPE html>
<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script language="javascript" type="text/javascript">
        <!--
function greeter() {

    var accx = 5;
    var accy = 6;
    var accz = 7;

    var output = [];
    output[0] = {
        name: "Accel_X",
        value: accx.toString(), // retrieve x
    };
    output[1] = {
        name: "Accel_Y",
        value: accy.toString(), // retrieve y
    };
    output[2] = {
        name: "Accel_Z",
        value: accz.toString() // retrieve z
    };

    var fromData = {};
    fromData.output = output;

    var fromDatan = JSON.stringify(fromData);
    alert(fromDatan);

    $.ajax({
        url: "http://posttestserver.com/post.php",
        type: "POST",
        headers: {

            "content-type": "application/json"
        },
        data: fromDatan,
        dataType: "JSON",
        success: function(fromDatan, status, jqXHR) {
            alert(JSON.stringify(fromData));
        },
        error: function(jqXHR, status) {
            alert(JSON.stringify(jqXHR));
        }
    });
    return false;
}
            //-->
    </script>
</head>

<body>
    <button onclick="greeter();">Click me</button>
</body>

</html>

I am unable to POST. I tried with POSTman chrome extension to POST data. It was working. WHy it is not working here I am not able to detect.

当我从$.ajax()调用中删除headers参数时,您的代码对我$.ajax()

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