Crystallize
Crystallize

Reputation: 110

Opening a new window with post request in an Asyncronous request

I need to send an Async POST request to a page that creates a PDF for me.

I have a PHP page that collects the data, and then sends an async (jQuery) trigger to process the data and convert it to JSON before sending it to another PHP page, always in Async mode.

The problem now is, i'm trying to use window.open to open a local page that creates the actual PDF and returns it to the browser.

Yet, window.open() does nothing, and the debugging console shows no error.

Code that should open the new window:

$_POST['ddt_data'] = $_SESSION['$refinedJSON']; ?>
<script>window.open("ddt_creation.php");</script>
<?php

I tried to open a blank window, nothing changes. The window doesn't open. I have no adblock active.


EDIT 1:

I've changed the code according to the suggestions in the comments below. The new code is now:

$_POST['ddt_data'] = $_SESSION['$refinedJSON'];
echo "BLAM"; ?>    
<script>alert(1); window.open("http://192.168.1.220/var/www/html/DDT_creation.php");</script>
<?php

I can see the echo in a return section that i have preparated for Async requests, but the alert box is never shown, and the same applies to the page i'm trying to open.

By checking the response i got from the request, i noticed it had something strange in it. This is the complete response:

BLAM    
<script>alert(1); window.open("http://192.168.1.220/var/www/html/DDT_creation.php");</script>

I know JS is working correctly (Using it in other parts of the same file) and i don't understand why the code is not executed immediately and is instead sent with the response.

Upvotes: 1

Views: 1971

Answers (1)

Coffee&#39;d Up Hacker
Coffee&#39;d Up Hacker

Reputation: 1446

The response of any AJAX request is always treated as text, unless told otherwise in which case the response is treated as the type you choose, for example JSON. This means that any JavaScript code in the response is never executed. You would need to add the code in the response to the page in order for it to get executed, but again, adding JavaScript code to a page by normal methods like innerHTML wouldn't get the code executed.

Instead, you should place the window.open code in the jQuery AJAX call, or from wherever in the code you have access to the response of the request.

Like so...

$.ajax({
    type: 'GET',
    url: 'http://example.com/',

    success: function(response) {
        window.open('http://192.168.1.220/var/www/html/DDT_creation.php');
    }
});

Upvotes: 1

Related Questions