0

I was implementing my code from this answer to do Ajax calls in webkit on a form upload and it works well. https://stackoverflow.com/a/16200886/548558

My backend gives me a redirect if the form is valid but the redirect is happen in the iframe but I really want to have it the normal window. How I can do this? Anybody has an idea?

CODE

$(function() {
    $('#upload-form').submit(function() {
        // Prevent multiple submits
        if ($.data(this, 'submitted')) return false;
        $(this).prop("target", "file-upload");
        // Update progress bar
        function update_progress_info() {
            // $progress.show();
            $.getJSON(progress_url, {'X-Progress-ID': uuid}, function(data, status) {
                if (data) {
                    var progress = parseInt(data.uploaded) / parseInt(data.length);
                    var width = $progress.find('.progress-container').width()
                    var progress_width = width * progress;
                    $progress.find('.progress-bar').width(progress_width);
                    $progress.find('.progress-info').text('uploading ' + parseInt(progress*100) + '%');
                    console.log("progress")
                    console.log(progress)
                    window.setTimeout(update_progress_info, freq);
                }
            });
        };
        window.setTimeout(update_progress_info, freq);

        $.data(this, 'submitted', true); // mark form as submitted
    });
});
Azd325
  • 5,752
  • 5
  • 34
  • 57

1 Answers1

2

If your call, if I understood your question, is made into an iframe, you must go to your window component, call to his parent (the container of this iframe) and call his methods location and href. I had to use this a few time ago and worked for me.

window.parent.location.href = "/foo.html"

DaGLiMiOuX
  • 889
  • 9
  • 28
  • If the form valid I get from my backend an redirect but this is happen in the iframe but I need it for the ajax calls. Where should I put it? – Azd325 May 17 '13 at 10:23
  • @Azd325 If you are recieving an `url` from your server, just add the code I posted into your success ajax function, replacing the value of `href` with the data you recieved from server. (i.e.) `...href = JSON.parse(data);` – DaGLiMiOuX May 17 '13 at 10:27
  • Ahh thansk, I'll try to do this. I will report. – Azd325 May 17 '13 at 10:29
  • but my ajax function is called multiple times to get the process of the upload. So I think it not possible to add it in the succes method – Azd325 May 17 '13 at 11:43
  • @Azd325 What? I don't understand what you meant. You have a form, with a submit button. When you press submit, you send a get request to your server. When your server sends the response, you should return a simple string, that will be parsed into Json format (something like `{"/foo.html"}`). When you recieve it in your success function into `data` parameter, the only thing you have to do is `window.parent.location.href = JSON.parse(data);`. Where is the problem/doubt? Correct me if you/your app are/is doing something different. – DaGLiMiOuX May 17 '13 at 12:00
  • Sorry, but now I got what you said. I have to think how I will implement it because I have one view for the progress and one for the page(this does the redirect) – Azd325 May 17 '13 at 12:44
  • Thanks for help but I think I have to solve somewhere else because the json response what you mention is also happen in the iframe. Maybe I check if the source location is changed of the iframe – Azd325 May 17 '13 at 13:07
  • Thanks I got it with this window.parent.location.href – Azd325 May 17 '13 at 13:33