简体   繁体   中英

Sending a POST request from an iframe, alerting the parent window href

I'm building a Rails App, but the client also has a wordpress component.

I'd like to be able to allow users to sign up from one of the wordpress pages.

To do that, first I set the x-iframe header in Rails to ALLOWALL.

Now, the signup process works, but after the user makes the post request, I'd like the base window url to change to whatever the response is.

Currently, I'm trying to do something like this ->

  jQuery(function($) {
    var timesRun = 0;
    var original = "http://localhost:3000/mainsignup"

    $('#sign-up-iframe').load(function(e){
      timesRun += 1
      if (timesRun != 1) {
        $(e.currentTarget).addClass('hidden')
        window.location.href = "http://localhost:3000/signup?iframe_error=catch"
      }
    });
  });

What this will do is that if the load event is called twice on this iframe(presumably because the user hits submit), I update the top window's location to go to the rails signup_path.

On Rails' ended, if the signup was successful, the signup_path redirects automatically to the user's profile. Otherwise, it renders the signup view, and inside it I have a generic error message, based on whether params[:iframe_error] exists.

This seems wrong though, and the user doesn't get specific error messages the first time. Is there a better way to do this?

The above solution seems to be the best I've found so far --

 jQuery(function($) {
    var timesRun = 0;
    var original = "http://localhost:3000/mainsignup"

    $('#sign-up-iframe').load(function(e){
      timesRun += 1
      if (timesRun != 1) {
        $(e.currentTarget).addClass('hidden')
        window.location.href = "http://localhost:3000/signup?iframe_error=catch"
      }
    });
  });

You let Rails handle the correct redirection, and after the second load of the iframe you redirect the person

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM