1
  1. I cant use AJAX (XMLHTTPRequest) because of Same-Origin-Policy.
  2. I cant access the Server internals, neither do I have the Server-Code.
  3. The Server only supports HTTPMethods: POST, OPTIONS
  4. I cannot use a third server (e.g. for using curl)

I can use a form-Post

<form method="post" action="some-web-server.com/something.JSON">
  <input name="param" value="some JSON param" />
</form>

to get the answer but I getting redirected to the JSON.page to get the answer. Is there a possibility to get the response in JS? something like that:

<form target="javascript: someFunctionUsingTheResponse(this.submit())" method="post" action="someServer/st.JSON" >

and in the HTML-Head

<script type="text/javascript">
  function someFunctionUsingTheResponse(text){
    alert(text);
  }
</script>

UPDATE: I already tried to create an <iframe name="dummy"> and setting the target of the form to this dummy-iframe <form target="dummy"> which works quite good (in Chrome, not working in IE), but i still cannot access the data of the iFrame using JS. (origin Policy)

Joel
  • 1,725
  • 3
  • 16
  • 34
  • do you have a server side part of your code? You could send the ajax request to your own server and do a `curl` from there to get the answer from the remote JSON and then return that value to your front-end. – martincarlin87 Jan 07 '16 at 09:25
  • `jsonp` is what you need – oleh.meleshko Jan 07 '16 at 09:25
  • `jsonp` is great but as far as I know the remote server has to support it or it won't work, i.e. wrap everything in the callback param, so it might not be an option for OP. – martincarlin87 Jan 07 '16 at 09:27
  • @martincarlin87 no, cant access the server-code, my own server would then need to be in the same domain as the `some-web-server`, right? I cannot access any server in this domain. – Joel Jan 07 '16 at 09:29
  • 1
    no, if you do the `curl` from the server side then it's fine even if it's a different domain (as long as you can access it and it's not blocked from the outside etc). An example of what curl is: http://stackoverflow.com/questions/3062324/what-is-curl-in-php – martincarlin87 Jan 07 '16 at 09:32
  • 1
    You can send request to your own server and then use curl to send request to another server. Request: browser->own server(ajax)->different server(curl) Response: browser<-own server(ajax response)<-different server(curl response) – Tanvir Jan 07 '16 at 09:35
  • @OlegMeleshko can I use JSONP with a POST-Request? – Joel Jan 07 '16 at 11:14
  • @martincarlin87 Unfortunately I am not allowed to use a third server, in order to use curl. – Joel Jan 07 '16 at 11:17

2 Answers2

3

Is there a possibility to get the response in JS?

No. A regular form submission does not make the data available to JS.

Any means of getting the data in JS will be subject to the same origin policy.

There are various ways to circumvent the same origin policy but a regular form submission to the target URL is not one of them.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-1

The form element should be like this:

<form onsubmit="someFunctionUsingTheResponse()">

You can use this as your function:

function someFunctionUsingTheResponse(){
    xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            alert(xmlhttp.responseText);
        }
    }
    xmlhttp.open("GET", "some-web-server.com/something.JSON", false );
    xmlhttp.send();
}
Dominick Navarro
  • 752
  • 6
  • 20