doumou1864 2016-02-04 22:49
浏览 44
已采纳

XMLHTTPrequest发送空帖

I've done a lot of times before, so I'm honestly confused why this is failing to pass anything. I've tried printing results (script gets a response and prints the file).

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);
}

My php is

print_r($_POST);

and my result is

XML: Array ()

Despite the fact that data passed in (which is double-checked right before being sent by my alert) is

{"reason":"get_stuff","build_name":"test"}
  • 写回答

1条回答 默认 最新

  • du4822 2016-02-04 22:53
    关注

    You said you were sending form encoded data.

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

    Then you sent:

    temp = JSON.stringify(data);
    

    JSON is application/json not application/x-www-form-urlencoded (and isn't natively supported by PHP anyway).

    Either encode your data as application/x-www-form-urlencoded or correct your content-type and parse it manually in PHP.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题