javascript - Ajax in jQuery not sending the correct Key-Value to PHP formular -


given 2 pieces of code:

javascript:

/*  stores received song. */ var receivedsong;  /*  class: goearsong            *  *  description: stores 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.'");'; ?> 

i cannot understand why not able correct output when given correct 'code' value. function exits error...

the php code correct has been tested in local server following string:

/localhost/goearscript.php?id=06b3682 

can shed light on issue? it's driving me mad!

thanks in advance!

edit

it seems php file not under root directory under /php/ one. problem solved changing this:

$.ajax({         type: 'get',         url: 'goearscript.php', 

to this:

$.ajax({         type: 'get',         url: 'php/goearscript.php', 

and changing line mentioned chosen answer. everybody!

you using $_get['id'] in php file , passing key get parameter, need pass id , add datatype: 'text',

replace data: {key: code}, data: {id: code}, , try

updated function getgoearlink below:

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

Comments