Ajax将数据传递到php脚本
我正在尝试将数据发送到我的PHP脚本来处理一些东西并生成一些东西。
$.ajax({      type: "POST",  
    url: "test.php", 
    data: "album="+ this.title,
    success: function(response) {
        content.html(response);
    }
});
在我的PHP文件中,我尝试检索专辑名称。虽然当我验证它时,我创建了一个警报以显示什么albumname都没收到,但是我尝试通过$albumname =
$_GET['album'];
虽然会说undefined:/
回答:
您正在发送POST AJAX请求,因此请$albumname =
$_POST['album'];在您的服务器上使用来获取值。我也建议您这样编写请求,以确保正确的编码:
$.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请求:
$.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设置。
以上是 Ajax将数据传递到php脚本 的全部内容, 来源链接: utcz.com/qa/422773.html








