Jump to content

Cross-domain call from JS to PHP

- - - - -

  • Please log in to reply
5 replies to this topic

#1
speculatius

speculatius

    Newbie

  • Members
  • PipPip
  • 25 posts
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

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

Quote

The problem is in point 3. How can I know, that response has already arrived?
Ehm, if you use ajax you must have this somewhere in your code:

  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
speculatius

speculatius

    Newbie

  • Members
  • PipPip
  • 25 posts
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.

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You can send ajax request to places of not the same origin. Google for "Cors" (Cross-Origin Resource Sharing)

#6
speculatius

speculatius

    Newbie

  • Members
  • PipPip
  • 25 posts
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