使用ajax请求获取php数据
嘿,我正在尝试使用ajax获取一个php文件并运行一些函数 我的ajax如下所示:使用ajax请求获取php数据,php,jquery,ajax,function,Php,Jquery,Ajax,Function,嘿,我正在尝试使用ajax获取一个php文件并运行一些函数 我的ajax如下所示: jQuery(document).ready(function(){ jQuery(".claim-button").click(function(){ alert("click ok"); var baseUrl = document.location.origin; jQuery.ajax({ type: "POST",
jQuery(document).ready(function(){
jQuery(".claim-button").click(function(){
alert("click ok");
var baseUrl = document.location.origin;
jQuery.ajax({
type: "POST",
url: '/custom_functions/win_checker.php'
});
});
});
然后在php文件中,我放置了一些简单的jsut来测试它的工作情况:
echo "Test";
我得到jquery“单击ok”,但它不返回php回音
我是阿贾克斯的新手,请有人告诉我我做错了什么
非常感谢
David您需要
成功
才能捕获PHP响应:
jQuery(document).ready(function(){
jQuery(".claim-button").click(function(){
alert("click ok");
var baseUrl = document.location.origin;
jQuery.ajax({
type: "POST",
url: '/custom_functions/win_checker.php',
data: {var1: 'someValue'},
success: function(response){
alert(response); // this will alert 'Test'
}
});
});
});
在PHP中,您将在
$\u POST
中获得参数。要处理AJAX请求的输出,您必须提供一个函数,以便在成功时执行:
jQuery(document).ready(function(){
jQuery(".claim-button").click(function(){
alert("click ok");
var baseUrl = document.location.origin;
jQuery.ajax({
type: "POST",
url: '/custom_functions/win_checker.php',
success: function(result) {
// Do with your result what you want, e.g. print it to the console
console.log(result);
}
});
});
});
检查控制台是否有错误您必须在ajax函数中为get response添加“success”函数“但它不会返回php回音”-不,您没有对响应执行任何操作。阅读如何使用jQuery.ajax,以及如何处理服务器的响应。展示您的表单非常感谢,但是如果您希望以otehr方式传递数据,例如在将ajax发送到php页面时,通过字符串或变量发送数据,您如何发送此数据,然后如何使用php获得此数据,请衷心感谢您的帮助