Javascript ajax调用don';我没有php响应
我的ajax调用有问题,但我看不到。首先,在前面的文件中,我有javascript:Javascript ajax调用don';我没有php响应,javascript,php,ajax,Javascript,Php,Ajax,我的ajax调用有问题,但我看不到。首先,在前面的文件中,我有javascript: someButton.addEventListener('click', function(){ makeRequest('arg') }); function makeRequest(params){ var http_request = false, url = 'actions.php'+'?state='+params; if (window.
someButton.addEventListener('click', function(){ makeRequest('arg') });
function makeRequest(params){
var http_request = false,
url = 'actions.php'+'?state='+params;
if (window.XMLHttpRequest){
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
}else if(window.ActiveXObject){
try{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
}
}
if (!http_request) {
console.log('Fail :( problem1');
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open('GET', url, true);
http_request.send(null);
function alertContents(){
if (http_request.readyState == 4) {
if (http_request.status == 200) {
console.log(http_request.responseText);
console.log(http_request.responseXML);
} else {
console.log('problem2.');
}
}
}
}
在action.php中,我有:
<?php return 'bla'; ?>
它返回:
XMLHttpRequest { onreadystatechange: alertContents(), readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, responseURL: "http://localhost/ordertask/task_act…", status: 200, statusText: "OK", responseType: "", response: "" }
将action.php更改为
<?php echo'bla'; ?>
请查看文档:。我希望这有助于。。。。这应该行得通。试试这段代码
<?php
echo'bla';
exit;
?>
我认为问题出在php文件中,请不要使用“return”语句,而是尝试诸如“print('bla');”或“echo'bla';”之类的打印操作。现在,当Ajax处理从php返回的消息时,至少应该读取“bla”并将其放在console.log()中。另外,为了安全起见,从末尾删除?>
。这是最好的做法。它可以防止意外拖尾浪费字符。哦,请在echo
和'bla'
之间留出一个空格好的,答案很简单,而且与“echo”或“print”完美配合,我不知道为什么我会把重点放在“return”上。谢谢你的回答
<?php
echo'bla';
exit;
?>