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)

8
  • 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. Commented Jan 7, 2016 at 9:25
  • jsonp is what you need Commented Jan 7, 2016 at 9: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. Commented Jan 7, 2016 at 9:27
  • 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: stackoverflow.com/questions/3062324/what-is-curl-in-php Commented Jan 7, 2016 at 9: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) Commented Jan 7, 2016 at 9:35

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

-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(); } 

1 Comment

First Line of the Question: Cant use AJAX, because of Same-Origin Policy

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.