Ajax将数据传递给php脚本

Nee*_*elp 12 php ajax jquery

我正在尝试将数据发送到我的PHP脚本来处理一些东西并生成一些项目.

$.ajax({  
    type: "POST",  
    url: "test.php", 
    data: "album="+ this.title,
    success: function(response) {
        content.html(response);
    }
});
Run Code Online (Sandbox Code Playgroud)

在我的PHP文件中,我尝试检索专辑名称.虽然当我验证它时,我创建了一个警报,以显示albumname我什么都没得到,我试图获取专辑名称$albumname = $_GET['album'];

虽然它会说未定义:/

Dar*_*rov 43

您正在发送POST AJAX请求,因此请$albumname = $_POST['album'];在服务器上使用以获取值.另外,我建议你写这样的请求,以确保正确的编码:

$.ajax({  
    type: 'POST',  
    url: 'test.php', 
    data: { album: this.title },
    success: function(response) {
        content.html(response);
    }
});
Run Code Online (Sandbox Code Playgroud)

或以其较短的形式:

$.post('test.php', { album: this.title }, function() {
    content.html(response);
});
Run Code Online (Sandbox Code Playgroud)

如果你想使用GET请求:

$.ajax({  
    type: 'GET',
    url: 'test.php', 
    data: { album: this.title },
    success: function(response) {
        content.html(response);
    }
});
Run Code Online (Sandbox Code Playgroud)

或以其较短的形式:

$.get('test.php', { album: this.title }, function() {
    content.html(response);
});
Run Code Online (Sandbox Code Playgroud)

现在在您的服务器上,您将能够使用$albumname = $_GET['album'];.使用AJAX GET请求时要小心,因为某些浏览器可能会缓存这些请求.为避免缓存它们,您可以设置cache: false设置.


rcr*_*ens 12

尝试发送如下数据:

var data = {};
data.album = this.title;
Run Code Online (Sandbox Code Playgroud)

然后你就可以像访问它一样

$_POST['album']
Run Code Online (Sandbox Code Playgroud)

注意不是'GET'