Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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 ajax发送数据_Javascript_Ajax - Fatal编程技术网

使用本机javascript ajax发送数据

使用本机javascript ajax发送数据,javascript,ajax,Javascript,Ajax,我使用的是本机javascript ajax,但当我看到发布的内容时,它只是说[object object],因此尝试在php文件中捕获$\u请求['stringSent'],没有任何效果 下面是我正在尝试的,叫做使用dataPost('test') 函数dataPost(dataToSend){ var params={“stringSent”:dataToSend}; var xmlhttp=new XMLHttpRequest(); xmlhttp.onreadystatechange=函

我使用的是本机javascript ajax,但当我看到发布的内容时,它只是说
[object object]
,因此尝试在php文件中捕获
$\u请求['stringSent']
,没有任何效果

下面是我正在尝试的,叫做使用
dataPost('test')

函数dataPost(dataToSend){
var params={“stringSent”:dataToSend};
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
如果(成功类型==“函数”){
警报(xmlhttp.responseText);
成功(xmlhttp.responseText);
}
}else if(typeof error==“function”&(xmlhttp.status>299 | | xmlhttp.status<200)){
错误();
}
}
open(“POST”,'dataCapture.php',true);
setRequestHeader('Content-Type','application/json;charset=UTF-8');
//xmlhttp.send(JSON.stringify(data));//不需要,因为我正在发送JSON
xmlhttp.send(params);
}
有更好的方法吗?

评论这一行:

// xmlhttp.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
或将其更改为:

xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send('stringSent=' + dataToSend);
使用jqueryajax

$.ajax({
url: 'dataCapture.php',
dataType: 'text',
type: 'post',
contentType: 'application/x-www-form-urlencoded',
data: params ,
success: function( data, textStatus, jQxhr ){ $('#response pre').html( data    ); },
error: function( jqXhr, textStatus, errorThrown ){ console.log( errorThrown ); }
}); 

他们为什么要这样做?
$.ajax({
url: 'dataCapture.php',
dataType: 'text',
type: 'post',
contentType: 'application/x-www-form-urlencoded',
data: params ,
success: function( data, textStatus, jQxhr ){ $('#response pre').html( data    ); },
error: function( jqXhr, textStatus, errorThrown ){ console.log( errorThrown ); }
});