0

I am new to web programming.Previously I worked in java module.I want to get http response by hitting particular url.I am looking in w3c schools and I am trying still no result.please help me I want to full webpage source.

code:

<html>
<head>
<script src="jquery.js">
</script>
<script>
var response = '';
$.ajax({ type: "GET",   
         url: "http://www.google.com",   
         async: false,
         success : function(text)
         {
             response = text;
         }
});

alert(response);
</script>

</head>
<body>
<input type="button" value="submit" />
<p>hello how are you </p>
</body>

</html>
6
  • You can use the object data recieved after success of the ajax to retrive the url in response Commented Mar 25, 2014 at 9:53
  • The var response doesn't exist out the ajax call, you need to use it insice de success call event. Commented Mar 25, 2014 at 9:54
  • are you getting an alert with this code ? Commented Mar 25, 2014 at 9:54
  • I am not getting anything inside also Commented Mar 25, 2014 at 9:55
  • You browser has a JavaScript error console. Use it. Commented Mar 25, 2014 at 10:03

2 Answers 2

4

This is because of the Same Origin Policy

From the JQuery ajax method documentation:

Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, port, or protocol.

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

4 Comments

if i change url also I am not getting any response
It has to be a path relative to the page on which the JavaScript code is running.
I have one website url which contains json response so that how can I get that json response.
Is that URL on the same domain as the JavaScript?
0

You can also add error callback and debug it what happening.

$.ajax({ type: "GET",   
     url: "http://www.google.com",   
     async: false,
     success : function(data, textStatus)
     {
         alert(data);
         alert(textStatus);
     },
     error : function(XMLHttpRequest, textStatus, errorThrown)
     {
         alert(XMLHttpRequest);
         alert(textStatus);
         alert(errorThrown);
     }

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy 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.