gpt4 book ai didi

javascript - 如何在不重新加载页面的情况下使用 GET 发送变量

转载 作者:行者123 更新时间:2023-11-30 22:43:56 25 4
gpt4 key购买 nike

我想要完成的是以下内容。我有一个 php 页面,它从 mysql 数据库加载一些变量。然后我想使用 GET 将这些变量发送到另一个页面,而不打开变量发送到的页面。

起初我真的不知道该怎么做,直到我遇到以下问题:

            $( document ).ready(function()
{
$.ajax({
url: 'the_url',
type: 'GET',
data: {Gender:Male,DateOfBirth:1968-07-21},
success: function(data) {
//called when successful
alert("Succes");
},
error: function(e) {
//called when there is an error
console.log(e.message);
alert("Failed");
}
});
}

$test$test1 是我想发送到其他页面的 php 变量。但显然我做错了什么。甚至没有触发警报,所以我的语法可能有问题。

我正在使用以下 jquery 版本:jquery-2.1.4.min.js

如果我问这个问题的方式有问题,请告诉我并帮助我更新问题。

最佳答案

只需将 PHP 变量赋给 JS 变量,并更改数据发送部分,不需要 ' 发送数据。

$( document ).ready(function()
{
var test = '<?php echo $test; ?>';
var test1 = '<?php echo $test1; ?>';
$.ajax({
url: 'the_url',
type: 'POST', //change type to POST rather than GET because this POST method of sending data
data: {test:test,test1:test1},
success: function(data) {
//called when successful
$('#ajaxphp-results').html(data);
alert("succes");
},
error: function(e) {
//called when there is an error
console.log(e.message);
alert("failed");
}
});
}

关于javascript - 如何在不重新加载页面的情况下使用 GET 发送变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30216034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com