2013-04-18 14 views
5

Usando l'ultimo jQuery (1.9.0), sono confuso sul motivo per cui questo codice non funziona:jQuery Ajax Get Errore di dati di sintassi, l'espressione non riconosciuta:

$.testAjaxFilter = function() { 

    var base = this; 

    // get faq categories 
    var currentFaqCategories = $('#category-list ul li a'); 

    // loop through each faq category link and bind a click event to each 
    if (typeof currentFaqCategories !== 'undefined') { 

     $.each(currentFaqCategories, function(index, category) { 

      $(category).click(function(e) { 
       $(e.target).getFaqList(); 
       return false; 
      }); 

     }); 

    } 

    // GET faq list elements from category link 
    $.fn.getFaqList = function() { 

     $.get($(this[0]).attr('href'), function(data) { 

      base.addFaqSectionToPage($(data).find('section.faq-page #content-column')); 

     }); 

    }; 

    // add new faq section to current page 
    this.addFaqSectionToPage = function(faqSection) { 

     // remove old faq section 
     var currentFaqSection = $('section.faq-page #content-column'); 

     currentFaqSection.empty(); 
     currentFaqSection.append(faqSection); 

    }; 

}; 

$.testAjaxFilter(); 

Durante la visualizzazione della console, su clic su uno dei collegamenti di categoria, GET recupera l'intera pagina nella sua risposta, ma viene quindi seguita da un errore di sintassi, espressione non riconosciuta: (elenca tutto l'HTML dalla pagina recuperata). Quindi, qualcosa sta andando storto in $.fn.getFaqList, probabilmente l'uso di $(data)?

C'è qualcosa di ovvio che sto sbagliando? Qualsiasi aiuto sarebbe molto apprezzato. Non sono molto esperto con le cose AJAX.

+0

Fare un .find ('a') – karthikr

+5

Se siete su 1.8+ potete provare '$ ($. ParseHTML (dati)). Find' – Musa

+0

Grazie Musa! L'ha fatto! Lo aggiungerò alla mia domanda – beefchimi

risposta

10

Grazie a Musa per la puntualizzazione. Vorrei premiare voi come la risposta corretta ... ma mi sa che non posso farlo per un commento :(

ho dovuto sostituire:

$(data).find 

con questo:

$($.parseHTML(data)).find 

Avevo provato qualcosa di simile prima, sulla base di altre risposte StackOverflow, ma non lo stavo eseguendo correttamente quindi continuavo a ricevere l'errore.Per chi ha bisogno di maggiore chiarezza sul problema, queste risposte potrebbero aiutare:

JQuery unrecognized expression on Ajax response

jQuery + client-side template = "Syntax error, unrecognized expression"

+0

'$ ($ .parseHTML (dati)). Find' ha funzionato per me ... –

+0

Funziona Ottimo lavoro! –

0

L'errore di sintassi potrebbe essere qui:

if (typeof currentFaqCategories !== 'undefined') { 

Che dovrebbe leggere:

if (typeof currentFaqCategories != 'undefined') { 
+0

Entrambi sono operatori validi. Fanno solo cose diverse. – shishirmk

Problemi correlati