jQuery中的Ajax未向PHP公式发送正确的键值
给出这两段代码: Javascript:jQuery中的Ajax未向PHP公式发送正确的键值,php,javascript,jquery,ajax,key-value,Php,Javascript,Jquery,Ajax,Key Value,给出这两段代码: Javascript: /* Stores the received song. */ var receivedSong; /* Class: goearSong * * Description: Stores a song */ function goearSong(link, artist, title) { this.link = link; this.artist = artist; this.title = t
/* Stores the received song. */
var receivedSong;
/* Class: goearSong *
* Description: Stores a song */
function goearSong(link, artist, title) {
this.link = link;
this.artist = artist;
this.title = title;
}
function getGoearLink(code) {
var key = 'id';
var value = code;
$.ajax({
type: 'GET',
url: 'goearScript.php',
data: {key : code},
success: function (response) {
alert("Success!");
},
error: function (response) {
alert("Error..." + response);
},
failure: function (response) {
alert("Failure..." + response);
}
});
}
getGoearLink("06b3682");
PHP:
有人能解释一下这个问题吗?它快把我逼疯了
提前谢谢
编辑
PHP文件似乎不在根目录下,而是在/PHP/one下。因此,通过改变这一点,问题得以解决:
$.ajax({
type: 'GET',
url: 'goearScript.php',
为此:
$.ajax({
type: 'GET',
url: 'php/goearScript.php',
同时也改变了所选答案中提到的行。谢谢大家 您在
PHP
文件中使用$\u GET['id']
,并且将键作为GET
参数传递,您需要传递id
并添加数据类型:'text',
将此数据:{key:code},
替换为此数据:{id:code},
,然后重试
更新的函数getGoearLink
如下所示:
function getGoearLink(code) {
$.ajax({
type: 'GET',
url: 'goearScript.php',
dataType: 'text',
data: {'id': code},
success: function(response) {
alert("Success!\n" + response);
},
error: function(response) {
alert("Error..." + response);
},
failure: function(response) {
alert("Failure..." + response);
}
});
}
它会出现什么错误?我刚刚做了,我担心它不起作用。。。现在看起来像数据:{id:code},但我仍然得到错误输出:SIt为我工作,您使用的是哪个版本的jQuery
?您完全正确!我犯了一个愚蠢的错误。谢谢在ajax
参数中添加dataType:'text',
,更新答案,将getGoearLink
替换为上述内容
$.ajax({
type: 'GET',
url: 'php/goearScript.php',
function getGoearLink(code) {
$.ajax({
type: 'GET',
url: 'goearScript.php',
dataType: 'text',
data: {'id': code},
success: function(response) {
alert("Success!\n" + response);
},
error: function(response) {
alert("Error..." + response);
},
failure: function(response) {
alert("Failure..." + response);
}
});
}