Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript XMLHttpRequest将有效负载附加到uri_Javascript_Json_Xmlhttprequest - Fatal编程技术网

Javascript XMLHttpRequest将有效负载附加到uri

Javascript XMLHttpRequest将有效负载附加到uri,javascript,json,xmlhttprequest,Javascript,Json,Xmlhttprequest,我正在做一个简单的POST-XMLHttpRequest,它成功地发布了数据,同时也将paylod附加到了URL中。是否有数据没有附加到URL var payLoad= JSON.stringify(ItemJSON); var xmlhttp = new XMLHttpRequest();

我正在做一个简单的POST-XMLHttpRequest,它成功地发布了数据,同时也将paylod附加到了URL中。是否有数据没有附加到URL

var payLoad= JSON.stringify(ItemJSON);                                                                      
var xmlhttp = new XMLHttpRequest();                                                                 
xmlhttp.addEventListener("readystatechange", function () {                                          
  if (xmlhttp.readyState < 4){                                                                      
    }                                                                                               
    else if (xmlhttp.readyState === 4) {                                                            
      if (xmlhttp.status == 200 && xmlhttp.status < 300){                                         
            console.log("Success",xmlhttp.responseText);                                            
        }                                                                                          
    }else{                                                                                          
        alert("Error!! \\n There was an error while saving. Please retry again later.");        
                }                                                                                           
        });                                                                                                 
        xmlhttp.open("POST", URL, false);                                                                   
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");                       
        xmlhttp.send(payLoad);  
}
var payLoad=JSON.stringify(ItemJSON);
var xmlhttp=new XMLHttpRequest();
addEventListener(“readystatechange”,函数(){
如果(xmlhttp.readyState<4){
}                                                                                               
如果(xmlhttp.readyState==4){
如果(xmlhttp.status==200&&xmlhttp.status<300){
log(“Success”,xmlhttp.responseText);
}                                                                                          
}否则{
警报(“错误!!\\n保存时出错。请稍后重试。”);
}                                                                                           
});                                                                                                 
open(“POST”,URL,false);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
发送(有效载荷);
}
答复:
这是因为您在发布内容时使用的标题:

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

xmlhttp.setRequestHeader("Content-Type", "application/json");

您的参考资料:

感谢Sujith提供的帮助。这里的另一点是我在发布表单数据。但当我移除表单时,它起了作用。