Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    [jQuery] - XML parsererror inspiegabile!

    Ciao a tutti, ho creato un picclissimo script che carica un XML tramite jQuery, dopo aver cliccato sul bottone di ricerca di un form, ecco il codice

    codice:
    <html>
    <head>
    	<title>Prova ricerca</title>
    	<script type="text/javascript" src="jquery.js"></script>
    	<script type="text/javascript">
    		$(function() {
    			$('#submitButton').click(function() {
    				var query = $("#query").val();
    				$.ajax({
    					type: 'POST', 
    					url: 'test.php', 
    					data: 'q=' + query + '&p=1&rpp=10', 
    					dataType: 'xml', 
    					error: function(request, type) {
    						$('#filling').html(request.responseText);
    						alert(type);
    					}, 
    					success: function(xml) {
    						$(xml).find('label').each(function() {
    							var prova = $(this).find('name').text();
    							$('#filling').append(prova + "
    ");
    						});
    					}
    				});
    			});
    		});
    	</script>
    </head>
    <body>
    <input type="text" id="query">
    <input type="submit" value="cerca" id="submitButton">
    <div id="filling"></div>
    </body>
    </html>
    Ecco invece un piccolo esempio dell'XML di risposta:

    codice:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <root>
     <labels>
       <label>
         <name>EzraPound</name>
         <address>
           <street>45UsuraPlace</street>
           <city>Hailey</city>
           <province>ID</province>
         </address>
       </label>
     </labels>
    </root>
    Il grossissimo problema, che mi sta facendo dannare, è che il javascript mi scatena sempre l'eccezione error, quindi esegue error: function(request, type) { al posto della success!
    E non riesco a capire come mai!
    L'errore ritornato nell'alert è parsererror (per intenderci, la variabile type della function error) ... ma perchè?
    Avete qualche idea? Grazie anticipatamente!

  2. #2
    ho provato con questo codice che poi è
    uguale al tuo
    Codice PHP:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd"
    >
    <
    html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <
    script type="text/javascript" src="../jquery-1.2.3.js"></script>
    <script type="text/javascript">
            $(function() {
                $('#submitButton').click(function() {
                    var query = $("#query").val();
                    $.ajax({
                        type: 'POST', 
                        url: 'test.xml', 
                        data: 'q=' + query + '&p=1&rpp=10', 
                        dataType: 'xml', 
                        error: function(request, type) {
                            $('#filling').html(request.responseText);
                            alert(type);
                        }, 
                        success: function(xml) {
                            $(xml).find('label').each(function(){
                                var name_text = $(this).find('name').text();
                                $('#filling').append(name_text + "
    ");
                            });
                        }
                    });
                });
            });
        </script>
    </head>
    <body>
    <input type="text" id="query">
    <input type="submit" value="cerca" id="submitButton">
    <div id="filling"></div>
    </body>
    </html> 
    e con questo xml (perchè non usi utf-8 ?)
    Codice PHP:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <labels>
       <label>
         <name>EzraPound1</name>
         <address>
           <street>45UsuraPlace</street>
           <city>Hailey</city>
           <province>ID</province>
         </address>
       </label>
        <label>
         <name>EzraPound2</name>
         <address>
           <street>45UsuraPlace</street>
           <city>Hailey</city>
           <province>ID</province>
         </address>
       </label>
        <label>
         <name>EzraPound3</name>
         <address>
           <street>45UsuraPlace</street>
           <city>Hailey</city>
           <province>ID</province>
         </address>
       </label>
    </labels>
    e funziona quindi l'errore è altrove.

    Togli il tag root in questo caso la root è labels
    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  3. #3
    Io avevo un problema forse simile, quello che succedeva era che il file xml era giusto ma nn veniva riconosciuto come xml da jQuery, e quindi scatenava un errore.
    Ho risolto molto semplicemente aggiungendo:
    Codice PHP:
    header('Content-type: text/xml'); 
    in cima alla pagina php, in questo modo specifichi che la risposta inviata dal file è un file xml.

    Spero ti sia utile,
    Ciao

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.