14

I want to get the HTML response page from the cross-domain URL.

for this I am using the ajax request as,

 $.ajax({ type: 'GET', url: "http://wcidevapps.com/salescentral/idisk/0001000383/iDisk", dataType: "json", success: function (response) { $(response).find('li a').each(function () { listHref.push($(this).attr('href')); }); } }); 

But after requesting it doesn't respond with any result back.

7
  • 3
    did you enable CORS? enable-cors.org Commented Mar 18, 2013 at 13:05
  • Ah, okay, I see. So, normally, JavaScript is not allowed to load stuff from other domains. This is called Same origin policy. But nowadays, you can get around this by configuring CORS or use tricks like JSONP Commented Mar 18, 2013 at 13:12
  • Don't worry about CORS, as JSONP is'nt really an ajax request you don't need it. The problem is that the adress you are using for the request does'nt return JSONP at all ? Commented Mar 18, 2013 at 13:16
  • 1
    have you tried any of the answers, yet? Commented Mar 18, 2013 at 19:56
  • yes I have tried this answer but not working Commented Mar 19, 2013 at 11:57

4 Answers 4

7
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script> <script type="text/javascript"> function NameAFunctionName() { $.ajax({ url: 'http://wcidevapps.com/salescentral/idisk/0001000383/iDisk', type: 'GET', dataType: 'json', headers: { //WRITE IF THEIR HAVE SOME HEADER REQUEST OR DATA }, crossDomain: true, success: function (data, textStatus, xhr) { console.log(data); }, error: function (xhr, textStatus, errorThrown) { console.log(errorThrown); } }); } </script> 
Sign up to request clarification or add additional context in comments.

Comments

5

Check documentation : http://api.jquery.com/jQuery.ajax/

crossDomain (default: false for same-domain requests, true for cross-domain requests)

Type: Boolean

If you wish to force a crossDomain request (such as JSONP) on the same domain, set the value of crossDomain to true. This allows, for example, server-side redirection to another domain. (version added: 1.5)

Comments

4

My suspicion is that you see the issue because the page you're requesting does not respond with a json(p) response, but responds with a redirect to:

http://wcidevapps.com/salescentral/idisk/0001000383/iDisk/ 

(note the trailing slash)

which then returns content type:

Content-Type:text/html;charset=ISO-8859-1 

Edit: If your intention is to retrieve the above site's data cross-domain, for further parsing by your script, I suggest that you choose one of the following:

Assumption 1: YOU are in control of the pages on server "http://wcidevapps.com"

In that case, you have two options: Either add CORS header "Access-Control-Allow-Origin: *" to the response (and configure the client ajax() call with dataType:"html"), or create a special JSON(P) page that delivers the same data as JSON (with padding) (and configure the client ajax() call like in the OP, with dataType:"jsonp")

Assumption 2: YOU are NOT in control of the pages on server http://wcidevapps.com

In that case, the only option I can think of is setup a proxy on a site that you control. Have that proxy "proxy" the requests/responses to "http://wcidevapps.com", but add the CORS header "Access-Control-Allow-Origin: *" to the response (and configure the client ajax() call with dataType:"html")

3 Comments

its not working because the data is not returned in JSON, and also give exception as "Uncaught SyntaxError: Unexpected token < "
you can also try this with my given site-url
yes, @Rahul, so I think the only options you have are the ones that I put in the edited answer
3

If you are using asp.net web service then you need to add this to webconfig file;

<system.webServer> <directoryBrowse enabled="true"/> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Headers" value="Content-Type" /> </customHeaders> </httpProtocol> </system.webServer> 

1 Comment

why directoryBrowse is enabled ? any reason

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.