1

I have a super simple safecracker form in my header (embedded template). It's literally 1 field. I'm using the ajax example in the ee docs (http://ellislab.com/expressionengine/user-guide/modules/safecracker/examples.html#ajax-driven-entry-form).

The form works everywhere except the home page. I get readyState: 4 which I read (https://stackoverflow.com/questions/5852257/jquery-s-ajax-error-handler-being-executed-if-readystate-4-and-status-200) means the returned json is malformed. It's true because the responseText is the code for the home page.

I've changed "Make the index template in this group your site's home page?" to a different template group and then the original home page works and this new temp home page fails.

My safecracker opening:

{exp:safecracker json="yes" 
channel="email_submission" id="email-subscribe" logged_out_member_id="2" 
datepicker="no" include_jquery="no" safecracker_head="no"}

And my js:

$('#email-subscribe').ajaxForm({
    dataType: 'json',
    success: function(data) {
        if (data.success) {
            alert('You successfully added a new entry with entry_id '+data.entry_id)
        } else {
            alert('Failed with the following errors: '+ JSON.stringify(data.errors));
        }
    },
    error: function(data) {
        // this is what's coming up
        alert('Failed with the following errors: '+ JSON.stringify(data));
    }
});

Has anyone encountered anything like this before? Any suggestions? I feel like I'm taking crazy pills.

Thanks Amanda

  • A few questions: If you hit view source in your browser, what is the <form>'s action attribute? When you encounter the error, does the entry get created in EE or not at all? I'd start by inspecting the response headers using Chrome web inspector or Charles.app. It kind of sounds like you're hitting some kind of 301 redirect when you submit the form, invalidating the POST of the form. – Rob Sanchez Aug 3 '13 at 13:07
1

Amanda, I started to set this up, and maybe have an idea for you from that, which may fix this.

When I set up the original example from the EE doc as a fresh template group's index page, it works, basically, when you call it as siteurl/group. However, if you call it as siteurl/group/index, it fails.

In the EE doc example, this is clearly because a segment is being used to pass an entry_id, and if you specify index, that string is passed which will be invalid.

Now, what I am thinking is that you aren't really using the example in your code above. You haven't specified an entry_id, and you are putting the code in an embed. My guess is that Safecracker may be checking segments or some EE variable on its own when you specify nothing; and that this doesn't work when the code is in an embed as the variable is not present, at least when you are on the home page in the normal fashion, and thus not using segments.

This idea might fit if I understand your observation from setting a different home page, because then you'll have to call the original with at least one segment.

Hence (with this handwaving) what I might suggest is to put your header with Safecracker code not in an embed, but in a Snippet, and call the snippet where you have been using the embed. This might allow Safecracker to find what it needs, as it will be parsed on the first level; and thus not fail when there are no declared segments as on an automatic home page.

Hope this works - in any case this has the scent of being close to identifying the problem, and if not, may bring out _rsan who wrote Safecracer I think, to give a better hint ;)

  • Away for long weekend but will try when I'm back. Thanks for thoughts! Will keep you posted. – Amanda Lutz Aug 3 '13 at 12:32
  • Ok, and be sure to try the things Rob Sanchez was good enough to suggest above if you don't get a quick fix from my thought -- he's the _rsan I mentioned who wrote it...! Interested how this turns out. – narration_sd Aug 3 '13 at 18:28
1

Thanks to @RobSanchez asking about the form's action attribute (which was always the url of the current page) I decided to cheat and give the ajaxForm a url that wasn't the home page.

No, it doesn't really explain anything but now it works.

Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.