2009-08-11 27 views
20

Ho problemi con il mio script jquery di seguito, questa è una versione ridotta di base e anche non funzionerà, ho il file php che lo script jquery effettua una chiamata per, ho impostato per codificare e mostrare una risposta JSONCome ottenere risposta jSON in variabile da uno script jquery

Quindi nello script jquery dovrebbe leggere il valore e rispondere ad esso ma non ottiene la risposta.

json.response è il modo sbagliato di chiamare una variabile nella stringa json che è la risposta del nome?

Qualcuno può aiuto per favore io sono bloccato

<?PHP 
header('Cache-Control: no-cache, must-revalidate'); 
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); 
header('Content-type: application/json'); 

// set to retunr response=error 
$arr = array ('resonse'=>'error','comment'=>'test comment here'); 
echo json_encode($arr); 
?> 

//the script above returns this: 
{"response":"error","comment":"test comment here"} 

<script type="text/javascript"> 
$.ajax({ 
    type: "POST", 
    url: "process.php", 
    data: dataString, 
    dataType: "json", 
    success: function (data) { 
     if (json.response == 'captcha') { 
      alert('captcha'); 
     } else if (json.response == 'error') { 
      alert('sorry there was an error'); 
     } else if (json.response == 'success') { 
      alert('sucess'); 

     }; 
    } 

}) 
</script> 

UPDATE;

ho cambiato
json.response

in

data.response

Ma questo non ce l'ha fatta ork sia

risposta

27

Ecco lo script, riscritto per utilizzare i suggerimenti sopra riportati e una modifica al metodo no-cache.

<?php 
// Simpler way of making sure all no-cache headers get sent 
// and understood by all browsers, including IE. 
session_cache_limiter('nocache'); 
header('Expires: ' . gmdate('r', 0)); 

header('Content-type: application/json'); 

// set to return response=error 
$arr = array ('response'=>'error','comment'=>'test comment here'); 
echo json_encode($arr); 
?> 

//the script above returns this: 
{"response":"error","comment":"test comment here"} 

<script type="text/javascript"> 
$.ajax({ 
    type: "POST", 
    url: "process.php", 
    data: dataString, 
    dataType: "json", 
    success: function (data) { 
     if (data.response == 'captcha') { 
      alert('captcha'); 
     } else if (data.response == 'success') { 
      alert('success'); 
     } else { 
      alert('sorry there was an error'); 
     } 
    } 

}); // Semi-colons after all declarations, IE is picky on these things. 
</script> 

Il problema principale qui era che si doveva un errore di battitura nel JSON si stavano tornando ("resonse" invece di "risposta". Questo significava che stavi cercando per la proprietà sbagliato nel codice JavaScript. Un modo di cattura di questi problemi in futuro è quello di console.log il valore di data e assicurarsi che la proprietà che stai cercando è lì.

Learning how to use the Chrome debugger tools (o strumenti analoghi in Firefox/Safari/Opera/etc.) sarà anche preziosa.

5

si dovrebbe usare data.response nei tuoi JS invece di json.response .

+0

Ho appena provato, ma non ha fatto un altro – JasonDavis

+0

puoi controllare di che tipo sono i tuoi dati. 'alert (typeof data);' nel callback di successo dovrebbe farlo. – RaYell

+0

dice "oggetto" – JasonDavis

4

vostro array PHP è definito come:

$arr = array ('resonse'=>'error','comment'=>'test comment here'); 

Avviso del mispelling "resonse". Inoltre, come menzionato da RaYell, devi utilizzare data anziché json nella tua funzione success perché il suo parametro è attualmente data.

Prova a modificare il file PHP per modificare il modulo di ortografia resonse a response. Dovrebbe funzionare allora.

+0

grazie che era il problema, l'ho preso in 1 area della sceneggiatura ma ho perso quella parte – JasonDavis

Problemi correlati