我正在尝试使用Ajax将脚本中的数据发送到PHP脚本。我有这个错误:注意:未定义的索引:背景。谢谢你的帮助。
ajax.php :
<?php
$mavariable2=$_POST['background'];
echo $mavariable2;
?>
script.js :
function open_script(){
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
var url = "ajax.php";
var back = "blue" ;
var backgroundf = "&background="+back;
hr.open("POST", url, true);
// Set content type header information for sending url encoded variables in the request
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
}
}
// Send the data to PHP now... and wait for response to update the status div
hr.send(backgroundf); // Actually execute the request
//window.location.assign('ajax.php');
}
答案 0 :(得分:0)
如果您已在项目中使用jQuery
作为依赖项,请尝试使用$.ajax()
。
var request = $.ajax({
url: "script.php",
method: "POST",
data: { background : 'blue' },
dataType: "html"
});
request.done(function( msg ) {
$( "#log" ).html( msg );
});