使用不带jQuery的XMLHttpRequest将JSON数据发送到PHP [英] Send JSON data to PHP using XMLHttpRequest w/o jQuery

查看:153
本文介绍了使用不带jQuery的XMLHttpRequest将JSON数据发送到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用XMLHttpRequest对象从表单发送JSON数据.我可以使用以下功能发送数据. FireBug中没有显示错误,并且FireBug可以很好地显示请求中的JSON数据.

I am trying to send JSON data from a form using the XMLHttpRequest object. I can send the data using the following function. There are no errors displayed in FireBug and the JSON-data in the request is displayed well formed by FireBug.

但是,我将数据发送到 echo.php ,它只是返回内容:

However, I send the data to echo.php, what simply returns the content:

<?php
print_r($_POST);
print_r($_GET);
foreach (getallheaders() as $name => $value) {
    echo "$name: $value\n";
}
echo file_get_contents('php://input');
?>

POST数组始终为空,但是我可以看到file_get_contents返回的JSON字符串.这是怎么发生的?我在做什么错了?

The POST-array is always empty, but I can see the JSON string returned by file_get_contents. How does that happen? What am I doing wrong?

echo.php的输出

Array
(
)
Array
(
)
Host: localhost
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: eo,de-de;q=0.8,de;q=0.6,en-us;q=0.4,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: application/json; charset=utf-8
Referer: http://localhost/form.html
Content-Length: 88
Cookie: {{..to much data..}}
Pragma: no-cache
Cache-Control: no-cache
{"type":"my_type","comment":"commented"}

发送功能:

function submit(){
    var data={};
    data.type=document.form.type.value;
    data.comment=document.form.comment.value;

    //get right XMLHttpRequest object for current browsrer
    var x=ajaxFunction();

    var string = JSON.stringify(data);

    x.open('POST','echo.php',true);
    x.setRequestHeader('Content-type','application/json; charset=utf-8');
    x.setRequestHeader("Content-length", string.length);
    x.setRequestHeader("Connection", "close");

    x.onreadystatechange = function(){
        if (x.readyState != 4) return;
        if (x.status != 200 && x.status != 304) {
            alert('HTTP error ' + req.status);
            return;
        }

        data.resp = JSON.parse(x.responseText);
        if(data.resp.status=='success'){
            alert('That worked!');
        }else{
            alert('That didn\'t work!');
        }
    }
    x.send(string);

    return false; //prevent native form submit
}

推荐答案

您忘记了在send函数中命名变量. 最好的使用方法是

You forgot to name your variables in the send function. The good way to use it is

x.send('name1='+string+'&name2=value2'); 

鉴于此,我认为您将不得不更改content-length标头.我认为发送它没有用.

Given that, I think you will have to change the content-length header. I don't think it is usefull to send it.

您可以做的另一件事是尝试使用GET方法. 您也可以尝试通过以下方法更改内容类型标题:

One another thing you can do is try with GET method. You can also try to change your content-type header by that one :

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")

这篇关于使用不带jQuery的XMLHttpRequest将JSON数据发送到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆