2016-02-04 116 views
0

我已经做了很多次,所以我老实地困惑,为什么这是无法通过任何东西。我试过打印结果(脚本得到一个响应并打印文件)。XMLHTTP请求发送空邮件

function submit_form_inpage(path, data, watchForChange){ 
    alert(data); 
    watchForChange = watchForChange || false; 
    var request = new XMLHttpRequest(); 
    request.open('POST', path, true); 
    request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); 
    if (watchForChange == true) { 
     request.onreadystatechange = function() { 
      if (request.readyState == 4) { 
       document.write(request); 
       if (request.status==200 && request.status < 400){ 
        var xmldata=request.responseText //retrieve result as an XML object 
        alert("XML:" + xmldata); 
       } 
       else{ 
        alert("An error has occured making the request:" + request.status); 
       } 
      } 
     } 
    } 
    var temp_string = array_to_string_for_post(data); 
    var temp = JSON.stringify(data); 
    alert(temp); 
    request.send(temp); 
} 

我的PHP是

print_r($_POST); 

,我的结果是

XML: Array() 

尽管在传递的数据(这是双重检查权由我的警报被发送之前)的事实

{"reason":"get_stuff","build_name":"test"} 

回答

1

你说你w发送表单编码数据。

request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); 

然后你发:

temp = JSON.stringify(data); 

JSON是application/json没有application/x-www-form-urlencoded(而不是原生PHP反正支持)。

要么你的数据编码为application/x-www-form-urlencoded或更正您的内容类型和parse it manually in PHP

+0

谢谢,我完全忘记了当我开始修改其他一些代码时,我已经把它留在了那里。这就是我用101度发烧编码得到的结果。 :P – liljoshu

+0

不幸的是,这并没有解决它,虽然这是一个需要改变。 – liljoshu

+0

@liljoshu - 你究竟发生了什么变化? – Quentin