php jQuery Ajax 返回整个页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2586217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery Ajax returns the whole page
提问by Sophia Gavish
I have a jquery-ajax function that sends data to a php script and the problem is with the return value, it returns the whole page instead of single value.
我有一个将数据发送到 php 脚本的 jquery-ajax 函数,问题在于返回值,它返回整个页面而不是单个值。
Thank you for your time and help.
感谢您的时间和帮助。
$("#ajaxBtn").click(function(){
var inputText = $("#testText").val();
$.ajax({ type: "POST",
url: "index.php",
data: "testAjax="+inputText,
dataType: "html",
success: function(html){
alert(html);
}
});
});
回答by ceejayoz
That's how it works. You're requesting index.phpvia AJAX, so you'll get whatever the contents of index.phpare.
这就是它的工作原理。您index.php通过 AJAX请求,因此您将获得任何内容index.php。
If you want a particular value, make a new PHP page that outputs just that value, and request that URL's contents instead.
如果您需要特定值,请创建一个仅输出该值的新 PHP 页面,并改为请求该 URL 的内容。
回答by Felix Kling
So when you have this on your index.phpat the beginning?:
那么当你index.php一开始就拥有这个时?:
<?php
if (isset($_POST["testAjax"])) {
echo $_POST["testAjax"];
}
?>
If this script is outputting further text then of course this will also be recieved by the Ajax call. You can put exit()after the echo to prevent the script from being processed further:
如果此脚本正在输出更多文本,那么 Ajax 调用当然也会接收到这些文本。您可以exit()在 echo 之后放置以防止脚本被进一步处理:
if (isset($_POST["testAjax"])) {
echo $_POST["testAjax"];
exit();
}
Also use dataType: 'text'as the value you return is obviously not HTML.
也dataType: 'text'用作您返回的值显然不是 HTML。
Or as others suggest, create a new page that is responsible for dealing with the Ajax request. If you are doing more than just outputting the received value (i.e. more complex operations) you should do this anyway.
或者按照其他人的建议,创建一个负责处理 Ajax 请求的新页面。如果您所做的不仅仅是输出接收到的值(即更复杂的操作),您无论如何都应该这样做。
回答by superUntitled
Sophia, In your case, you should not send your data to a php file that, if viewed by a web browser, displays a website page. You should send your information to a php file that only returns the data that you would like to see returned.
Sophia,在您的情况下,您不应该将您的数据发送到 php 文件,如果通过网络浏览器查看,则显示网站页面。您应该将您的信息发送到一个 php 文件,该文件只返回您希望返回的数据。
So instead of "index.php", create a file called something like "my-ajax-script.php". That file should include any necessary "include()" files to connect to the database and your php function files (ie: for sanitizing the POST data). Then the file should have the code that processes the POST data, and echo's out the data complete with some html tags. This data will be inserted into your existing DOM (html markup).
因此,不要创建一个名为“my-ajax-script.php”的文件,而不是“index.php”。该文件应包含任何必要的“include()”文件以连接到数据库和您的 php 函数文件(即:用于清理 POST 数据)。然后文件应该有处理 POST 数据的代码,并用一些 html 标签回显数据。此数据将插入到您现有的 DOM(html 标记)中。
回答by Sophie cai
In my case, anyone who has the same issue try using
就我而言,任何有同样问题的人都可以尝试使用
require_onceinstead of requirein your bootstrap php file to prevent a whole page re-render a second time through out the system.
require_once而不是require在您的引导 php 文件中,以防止整个页面在整个系统中第二次重新呈现。

