0

鉴于这两段代码:

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 = 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
function path($id){
  $ip = substr($id, 0, 1);
  $load = 'http://www.goear.com/tracker758.php?f='.$id.'';
  $load = file_get_contents($load);
  $temp = explode("=", $load);
  $num = 3;
  $path = $temp[$num];
  $path = str_replace("\"", "",$path );
  $path = str_replace(" bild", "",$path );
  return($path);
}
function name($id){
  $ip = substr($id, 0, 1);
  $load = 'http://www.goear.com/tracker758.php?f='.$id.'';
  $load = file_get_contents($load);
  $temp = explode("=", $load);
  $num = 5;
  $name = $temp[$num]." - ".$temp[$num+1];
  $name = str_replace("\" title", "",$name );
  $name = str_replace("\" />", "",$name );
  $name = str_replace("\"", "",$name );
  $name = str_replace("</songs>", "",$name );
  $name = str_replace("/>", "",$name );
  return($name);
}

$_id = $_GET['id'];
$_ip = substr($_id, 0, 1);
if($_id){
  $load = 'http://www.goear.com/tracker758.php?f='.$_id.'';
        $xml = @simplexml_load_file($load);
        if ($xml) {
            $path = $xml->song['path'];
            $artist = $xml->song['artist'];
            $title = $xml->song['title'];
            $name = $artist.' - '.$title.'';
            }
        else{
          $path = path($_id);
          $name = name($_id);
        }
}
echo 'new goearSong("'.$path.'", "'.$artist.'", "'.$title.'");';
?>

我无法理解为什么在给定正确的“代码”值时无法获得正确的输出。该函数总是作为错误退出......

PHP 代码是正确的,因为它已在我的本地服务器中使用以下字符串进行了测试:

/localhost/goearScript.php?id=06b3682

任何人都可以对这个问题有所了解吗?快把我逼疯了!

提前致谢!

编辑

好像PHP文件不在根目录下,而是在/php/下。所以问题是通过改变这个来解决的:

$.ajax({
        type: 'GET',
        url: 'goearScript.php',

对此:

$.ajax({
        type: 'GET',
        url: 'php/goearScript.php',

并更改所选答案提到的行。谢谢大家!

4

1 回答 1

0

$_GET['id']PHP文件中使用并且key作为GET参数传递,您需要传递id并添加dataType: 'text',

用这个替换data: {key: code},data: {id: code},并尝试

更新function 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);
        }
    });

}
于 2013-07-11T20:26:37.473 回答