【问题标题】:Raw javascript eq of ajax post is not working correctly [duplicate]ajax 帖子的原始 javascript eq 无法正常工作 [重复]
【发布时间】:2016-02-19 22:19:26
【问题描述】:

我正在尝试制作一些 ajax 发布原始 js 脚本,我正在使用 stackoverflow 中提出的问题之一,但我遇到了一个问题。

var r = new XMLHttpRequest(); 
r.open("POST", "###URL###", true);
r.onreadystatechange = function () {
    if (r.readyState != 4 || r.status != 200) return; 
        console.log(r.responseText);
    };
console.log(price);
r.send("prize=" + prize);
return false;

当我在 chrome 网络中检查有效负载时,正确发送了 Prize=632,但带有 $_POST['prize'] 的 php 脚本的结果为空。问题可能出在哪里?

最好的问候!

【问题讨论】:

    标签: javascript php ajax post


    【解决方案1】:

    您缺少标题:

    var data = "prize=" + prize;
    var r = new XMLHttpRequest(); 
    r.open("POST", "###URL###", true);
    
    // Send the expected headers information along with the request
    r.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    r.setRequestHeader("Content-length", data.length);
    r.setRequestHeader("Connection", "close");
    
    r.onreadystatechange = function () {
        if (r.readyState != 4 || r.status != 200) return; 
            console.log(r.responseText);
        };
    console.log(price);
    r.send(data);
    return false;
    

    【讨论】:

    • @g9m29 不客气!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    相关资源
    最近更新 更多