Sto cercando di imparare JQuery e ho un piccolo problema con ajax. Sto cercando di popolare un array javascript con valori restituiti da una risposta XML da una pagina.
Ecco la mia pagina principale (ajax.html):La funzione di errore JQuery ajax viene eseguita anche se la query ha esito positivo
<html>
<head>
<script type="text/javascript" src="jquery/jquery.js"></script>
<script type="text/javascript" src="jquery/fiber.js"></script>
</head>
<body>
<p>Ajax</p>
<script>
var ringType = new Array();
</script>
</body>
</html>
fiber.js è questa: (? Senza le barre rovesciate in \)
//process things for fiber map
jQuery(document).ready(function() {
// do stuff when DOM is ready
//populate and display ringType
$.ajax({
type: "GET",
url: "ajaxHelper.pl",
data: {
getRingTypes: "1",
},
dataType: "xml",
success: function(xml) {
//if the query was successfull,
alert("Got an xml object:"+$(xml));
$(xml).find("ringType").each(function(){
alert("Received reply "+$(this).text());
var type = $(this).html(); //save the value
//append to ringType array
ringType.push(type);
});
},
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
}
});
for(var i=0; i<ringType.length; i++){
document.write("<br>"+ringType[i]);
}
});
ajaxHelper.pl genera questo XML (come Content tipo text/xml):
<?xml version="1.0" encoding="ISO-8859-1"?>
<\?xml version="1.0" encoding="ISO-8859-1"\?>
<ringType>IA</ringType>
<ringType>IL</ringType>
<ringType>IN</ringType>
<ringType>IR</ringType>
<ringType>RT</ringType>
il problema è che ogni volta che carico ajax.html, la query Ajax è successo, ma viene eseguita la funzione di errore! xhr.status = 200 (significa che la query era ok) e thrownException non è definito.
Spiacenti, l'XML non incollare correttamente. Qui va di nuovo (con alcuni spazi): xml version = "1.0" encoding = "ISO-8859-1"? > < ringType > IA TipoSuon> < ringType > IL TipoSuon> < ringType > IN TipoSuon> < ringType > IR TipoSuon> < ringType > RT TipoSuon> – Adrian
Prova a mettere Xml version = "1.0" encoding =" UTF-8 "?> Nella parte superiore del documento XML di output, se non si è già – karim79
se si apre il file ajaxHelper.pl nel proprio browser (http: //mywebapp/ajaxHelper.pl), il browser riconosce il risposta dal file come XML? –