Search code examples
jqueryajaxexpressionengine

Parsing ExpressionEngine Error Template with Ajax


I'm trying to add Ajax to my ExpressionEngine registration form with the jQuery Form plugin. Most of the form errors are handled with Profile:Edit and returned in JSON, and I've had no major problems parsing those. The only error that isn't handled by Profile:Edit is when someone tries to re-register with an email address that is still "pending". For that, I get the standard EE error template in HTML.

What I'd like to do is set up a jQuery $.each() loop and output the error list item(s). But when I try to parse the HTML I get weird data in the console. Even a really simple jQuery function doesn't work like I expect. For example, with this code, I expect to get back the content of the #content div:

$('.myForm').ajaxForm({
    success: function(data){
        var theContent = data.find('#content');
        console.log(theContent);
    }
});

But I get this in the console:

Uncaught TypeError: Object
<!-- Full HTML of Error Template -->
has no method 'find' 

The same thing happens if I use .filter() instead of .find().

If I wrap data in selector syntax, like this:

$('.myForm').ajaxForm({
    success: function(data){
        var theContent = $(data).find('#content');
        console.log(theContent);
    }
});

I get this in the console:

[prevObject: e.fn.e.init[9], context: undefined, selector: "#content"]
    context: undefined
    length: 0
    prevObject: e.fn.e.init[9]
        0: #text
        1: <title>
        2: #text
        3: <meta>
        4: #text
        5: <style>
        6: #text
        7: <div>
        8: #text
        length: 9
    __proto__: Object[0]
    selector: "#content"
    __proto__: Object[0]

Which is not what I expected, nor do I really know what to do with it.

Setting the dataType as 'html' doesn't seem to make a difference.

Any thoughts? This thing has aged me by years, it's so damn frustrating.


Solution

  • data.find('#content');
    

    Won't work because data is not a jQuery object. What does the response coming back look like? If you do a console.log(data) upon success, what do you get?