Hello,
I want to do cross-domain communication from javascript client to PHP backend. It must be done via POST method (not GET), so I modified JSONP pattern. This is how I want to do one call between client and server:
1) JS client creates hidden form and post all input arguments to my PHP backend script (say input.php).
2) Server responses with '204 No Content', so client side is not redirected to output of this PHP script.
3) When 204 response is delivered to client, client will create hidden 'script' element. Attribute 'scr' points to server PHP script (output.php), where output of given service is already prepared.
4) On event script.onload, response can be parsed.
The problem is in point 3. How can I know, that response has already arrived? There is no event such as form.onSubmitCompleted. Have you ever tried to do this, and how?
Thank you
5 replies to this topic
#1
Posted 14 July 2011 - 10:30 PM
|
|
|
#2
Posted 15 July 2011 - 07:54 AM
The problem is in your entire plan. Once you submit the form, you are NO LONGER in the old page. As a result, there is no JS on the client to react to the 204 response.
I'm guessing that the purpose of all this is to somehow avoid cross-site AJAX calls that may get blocked by security, but the whole thing seems really bizarre.
I'm guessing that the purpose of all this is to somehow avoid cross-site AJAX calls that may get blocked by security, but the whole thing seems really bizarre.
#3
Posted 16 July 2011 - 12:29 AM
Quote
The problem is in point 3. How can I know, that response has already arrived?
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
So just change 200 into 204. (and maybe leave the readyState out)
#4
Posted 20 July 2011 - 11:10 PM
Thank you for yours answers. I make all this because of security issue called 'same origin policy'. You can not make request to different domain from JS, this is prohibited by browser. So you can not use XMLHttpRequest as well. The only way to post data is to submit <form target='some-named-frame'>. And the only way to receive data is to use <script src='url-to-request'>. This is reason, why I make this communication in more steps.
If you are interested in, I already solved my problem. First I post data to 'in.php' and immediately after this I set source of script to 'out.php'. On server-side, script out.php will wait for in.php to finish. So when client get script.onload event, it can be sure that data are ready for process.
If you are interested in, I already solved my problem. First I post data to 'in.php' and immediately after this I set source of script to 'out.php'. On server-side, script out.php will wait for in.php to finish. So when client get script.onload event, it can be sure that data are ready for process.
#5
Posted 21 July 2011 - 12:33 AM
You can send ajax request to places of not the same origin. Google for "Cors" (Cross-Origin Resource Sharing)
#6
Posted 21 July 2011 - 06:10 AM
Thank you Wim DC. This is probably what I was looking for :)
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









