Ajax 将数据传递给 php 脚本 [英] Ajax passing data to php script
问题描述
我正在尝试将数据发送到我的 PHP 脚本以处理一些内容并生成一些项目.
I am trying to send data to my PHP script to handle some stuff and generate some items.
$.ajax({
type: "POST",
url: "test.php",
data: "album="+ this.title,
success: function(response) {
content.html(response);
}
});
在我的 PHP 文件中,我尝试检索专辑名称.虽然当我验证它时,我创建了一个警报来显示 albumname
是什么我什么也没得到,我尝试通过 $albumname = $_GET['album'];代码>
In my PHP file I try to retrieve the album name. Though when I validate it, I created an alert to show what the albumname
is I get nothing, I try to get the album name by $albumname = $_GET['album'];
虽然它会说 undefined :/
Though it will say undefined :/
推荐答案
您正在发送 POST AJAX 请求,因此在您的服务器上使用 $albumname = $_POST['album'];
来获取价值.此外,我建议您编写这样的请求以确保正确编码:
You are sending a POST AJAX request so use $albumname = $_POST['album'];
on your server to fetch the value. Also I would recommend you writing the request like this in order to ensure proper encoding:
$.ajax({
type: 'POST',
url: 'test.php',
data: { album: this.title },
success: function(response) {
content.html(response);
}
});
或其较短的形式:
$.post('test.php', { album: this.title }, function() {
content.html(response);
});
如果您想使用 GET 请求:
and if you wanted to use a GET request:
$.ajax({
type: 'GET',
url: 'test.php',
data: { album: this.title },
success: function(response) {
content.html(response);
}
});
或其较短的形式:
$.get('test.php', { album: this.title }, function() {
content.html(response);
});
现在你可以在你的服务器上使用 $albumname = $_GET['album'];
.但是要小心使用 AJAX GET 请求,因为它们可能会被某些浏览器缓存.为了避免缓存它们,您可以设置 cache: false
设置.
and now on your server you wil be able to use $albumname = $_GET['album'];
. Be careful though with AJAX GET requests as they might be cached by some browsers. To avoid caching them you could set the cache: false
setting.
这篇关于Ajax 将数据传递给 php 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!