0

I have some javascript code that sends an ajax request, but I get no response. How am I suppose to fix this? Thanks!

The Link I was trying to fetch is: http://research.engineering.wustl.edu/~todd/cse330/demo/lecture6/helloClass.txt

My code:

<!DOCTYPE html> <html> <head> <script type="text/javascript"> function sendRequest() { alert("Sending request"); var xmlHttp = new XMLHttpRequest(); xmlHttp.open("GET","http://research.engineering.wustl.edu/~todd/cse330/demo/lecture6/helloClass.txt",true); xmlHttp.addEventListener("load",ajaxCallback,false); xmlHttp.send(null); } function ajaxCallback(event) { alert("Here is the response " + event.target.responseText ); document.getElementById("demo").innerHTML= event.target.responseText; } </script> </head> <body> <h1>My Ajax Web Page</h1> <p id="demo">I will display something here</p> <form> <input name="submit" type=button value="Send Ajax Request" onClick="sendRequest()"> </form> </body> </html> 

Thank you for your answer. I just tried JSONP as you suggested, I use

<script type="text/javascript" src="http://research.engineering.wustl.edu/~todd/cse330/demo/lecture6/helloClass.txt?jsonp=parseResponse"> 

for my new script tag but still not work. Any furthur hint? Thanks!

Thank you all for your answer! But I'm new to this technique,so still abit confused. Would you mind bring more detail code for me to study. And I'm really interested in the agent listening method.

3
  • You can't make cross domain requests in JavaScript. Commented Jul 20, 2013 at 0:30
  • @AustinBrunkhorst Actually, you can. Commented Jul 20, 2013 at 0:34
  • You can, but not out of the box as he was trying. Commented Jul 20, 2013 at 1:20

2 Answers 2

2

I assume that you are not writing this HTML on http://research.engineering.wustl.edu/. If so, then you have just had a brush with the Same Origin Policy, which disallows cross-domain requests.

Now there are ways to do this cross-domain communication, and some of the common ways are Cross-Origin Resource Sharing and JSONP. You can also make your server, the domain your page is living in, a proxy server which fetches the remote page for you, since server-code is not bound to SOP.

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

2 Comments

Thank you for your answer. I just tried JSONP as you suggested, I use <script type="text/javascript" src="research.engineering.wustl.edu/~todd/cse330/demo/lecture6/…"> for my new script tag but still not work. Any furthur hint? Thanks!
@user2514364 JSONp is has several parts. You need your loader function which creates the script file to point to your data, the receiver function which executes the data, and the remote file which is basically data wrapped in a receiver function call.
0

For start: you simply cannot make cross browser ajax requests.

JSON would work but will require a bit of recode of your javascript.

A solution: Relay the requests through your server. Create an agent on your server that listens to requests like: "/Relay?url=http://research.engineering.wustl.edu/~todd/cse330/demo/lecture6/helloClass.txt" and responds with any answer from that url. The coding on your server is simple and it will require a minimum of redesign in your javascript. If you have any questions on how to code your server, just ask.

3 Comments

In fact, there's no need to ask. StackOverflow is full of these questions. It's just a matter of search.
Thank you all for your answer! But I'm new to this technique,so still abit confused. Would you mind bring more detail code for me to study. And I'm really interested in the agent listening method.
The details in creating such agent depends on what server technique you are using, such as .NET, PHP, JAVA. What the agent should do is 1: Take an url as parameter. 2: Call that url. 3: Read the response. 4: Return the response. In that way, calling you agent instead of the original url, will render the same results, except that your not doing a cross browser request

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.