My goal is to allow for non logged in users to view a page with a bookmark button. When non logged in user clicks on the bookmark button, he should be presented a login form in a modal, submit their credentials and have the bookmark added (same way Yelp does it).
My problem is I can’t make the login form post the create bookmark request.
Here is my jQuery code.
$("form#login-form").submit(function(event) {
event.preventDefault();
$.post('/account/authenticate_xhr', $(this).serialize(), function(data, textStatus, jqXHR) {
console.log("posted authenticate");
if (data['user_id']) {
// log in successful
console.log("logged in!"+data['user_id']);
if (data['next_action']) {
// prepare and post next action
console.log('next action!');
var next_action = data['next_action'];
var controller = next_action['controller'];
var action = next_action['action'];
var data = next_action['data'];
$j.post('/' + controller + '/' + action, { data: data }, function(data, textStatus, jqXHR) {
}, "script");
}
};
}, "script");
console.log('ending');
});
When I run it, ONLY “ending” shows up in the console. I can’t execute the code inside the authenticate_xhr post request while it seems the request is executed properly on the back end. But the response authenticate_xhr show me the right JSON object…
What am I missing here?
UPDATES:
- I followed charlietfl suggestion and replaced ‘script’ with ‘json’ to call ‘/account/authenticate_xhr’ and now get the first post working fine. I now need to execute my js.erb response…
- Kept ‘script’ for the second post request and added escape_javascript in the js.erb view
You set the
dataTypeasscript…needs to bejson. Even if request is for jsonp that gets wrapped in a function, you still requestjson