AJAX 响应 returns 当前页面
AJAX response returns current page
我一直在搜索类似的问题,但是 none 的解决方案对我有用(但我找不到完全相同的问题)。
首先,我正在使用的网站是 运行 Zend Framework。我怀疑这与问题有关。
我想做一个非常基本的 AJAX 功能,但出于某种原因,我的响应总是等于当前页面的 html。我不需要 Zend 的任何功能,我需要实现的功能可以(并且我希望它们)与框架分开工作。
出于测试目的,我尽可能地简化了它,但我没有找到错误。我有一个页面 "test.php",其中只有一个 link 触发了 ajax 调用。此调用的外观如下:
$('.quiz-link').click(function(e){
e.preventDefault();
$.ajax({
URL: "/quiz_api.php",
type: "POST",
cache: false,
data: {
'test': 'test'
},
success: function(resp){
console.log(resp);
},
error: function(resp){
console.log("Error: " + reps);
}
});
});
而这个 quiz_api.php 只是:
<?php
echo "This is a test";
?>
当我点击 link 时,我得到了当前页面的整个 HTML。 "This is a test" 在那里找不到。我也收到一个错误:"Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/."
我认为它与此 HTML 响应中包含的 JS 文件有关,但我也尝试设置 "async: true" 但它没有帮助。
我想避免使用 Zend Framework 函数来完成这项任务,因为我不太熟悉它,甚至制作一个简单的控制器听起来也很痛苦。相反,我想找出导致此类行为的原因,看看是否可以更改。
PS:我也试过将 quiz_api.php 移动到另一个域,但没有任何改变。
我知道这可能是一个较旧的代码,但它可以工作,简单且适应性强。这是我想出的。希望对你有用。
//Here is the html
<a href="#" id="test" onclick="test()">Link Test</a>
<div id="test_div"></div>
function test(){
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
// This is the php file link
var url = "quiz_api.php";
// Attaches the variables to the url ie:var1=1&var2=2 etc...
var vars = '';
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;
console.log(return_data);
document.getElementById('test_div').innerHTML = return_data;
}else{
document.getElementById('test_div').innerHTML = "XMLHttpRequest failed";
}
}
//Send the data to PHP now... and wait for response to update the login_error div
hr.send(vars); // Actually execute the request
}
您可以使用 document.write 更改整个页面,而不是更改 dividual "div"s
我一直在搜索类似的问题,但是 none 的解决方案对我有用(但我找不到完全相同的问题)。
首先,我正在使用的网站是 运行 Zend Framework。我怀疑这与问题有关。
我想做一个非常基本的 AJAX 功能,但出于某种原因,我的响应总是等于当前页面的 html。我不需要 Zend 的任何功能,我需要实现的功能可以(并且我希望它们)与框架分开工作。
出于测试目的,我尽可能地简化了它,但我没有找到错误。我有一个页面 "test.php",其中只有一个 link 触发了 ajax 调用。此调用的外观如下:
$('.quiz-link').click(function(e){
e.preventDefault();
$.ajax({
URL: "/quiz_api.php",
type: "POST",
cache: false,
data: {
'test': 'test'
},
success: function(resp){
console.log(resp);
},
error: function(resp){
console.log("Error: " + reps);
}
});
});
而这个 quiz_api.php 只是:
<?php
echo "This is a test";
?>
当我点击 link 时,我得到了当前页面的整个 HTML。 "This is a test" 在那里找不到。我也收到一个错误:"Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/."
我认为它与此 HTML 响应中包含的 JS 文件有关,但我也尝试设置 "async: true" 但它没有帮助。
我想避免使用 Zend Framework 函数来完成这项任务,因为我不太熟悉它,甚至制作一个简单的控制器听起来也很痛苦。相反,我想找出导致此类行为的原因,看看是否可以更改。
PS:我也试过将 quiz_api.php 移动到另一个域,但没有任何改变。
我知道这可能是一个较旧的代码,但它可以工作,简单且适应性强。这是我想出的。希望对你有用。
//Here is the html
<a href="#" id="test" onclick="test()">Link Test</a>
<div id="test_div"></div>
function test(){
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
// This is the php file link
var url = "quiz_api.php";
// Attaches the variables to the url ie:var1=1&var2=2 etc...
var vars = '';
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;
console.log(return_data);
document.getElementById('test_div').innerHTML = return_data;
}else{
document.getElementById('test_div').innerHTML = "XMLHttpRequest failed";
}
}
//Send the data to PHP now... and wait for response to update the login_error div
hr.send(vars); // Actually execute the request
}
您可以使用 document.write 更改整个页面,而不是更改 dividual "div"s