阿贾克斯将数据传递到PHP脚本 [英] Ajax passing data to php script

查看:233
本文介绍了阿贾克斯将数据传递到PHP脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我试图将数据发送到我的PHP脚本来处理一些东西,并产生一些项目。

Hi 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 ['专辑']来获得专辑名称;

and 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'];

虽然它会说不确定:/

Though it will say undefined :/

推荐答案

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

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);
    }
});

或在较短的形式:

or in its shorter form:

$.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);
    }
});

或在较短的形式:

or in its shorter form:

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

现在您的服务器上,你西港岛线能够使用 $ albumname = $ _GET ['专辑']; 。不过要小心使用AJAX GET请求,因为它们可能会被某些浏览器缓存。为了避免缓存他们,你可以设置缓存:假设置

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.

这篇关于阿贾克斯将数据传递到PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆