Ajax将数据传递给php脚本

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

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

在我的PHP文件中,我尝试检索专辑名称。 虽然当我validation它时,我创建了一个警告,以显示我没有得到的专辑名称,我试图通过$albumname = $_GET['album'];获得专辑名称$albumname = $_GET['album'];

虽然它会说未定义:/

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

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

或以其较短的forms:

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

如果你想使用GET请求:

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

或以其较短的forms:

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

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

尝试发送如下数据:

 var data = {}; data.album = this.title; 

然后你可以像访问它一样

 $_POST['album'] 

注意不是’GET’

您还可以使用下面的代码来使用ajax传递数据。

 var dataString = "album" + title; $.ajax({ type: 'POST', url: 'test.php', data: dataString, success: function(response) { content.html(response); } });