使用本机javascript ajax发送数据
我使用的是本机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=函
[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 ); }
});