0

i'm creating a web application in asp.net in which i would like to give user a one JavaScript code, through which the user can get content from my website to his/her own website.

i've used one generic handler to process user request in my website.

so, now How can i do this, i've tried using jQuery.. jQuery can only process requests from current server, not from other server (if i'm not wrong)

code on www.otherwebsite.com

<script type="text/javascript" src="jquery.x.x.js">  </script> 
<script type="text/javascript"> 
    $('#result').load('www.myWebSite.com/getData.ashx');
</script>  
<body>  
    <div id="result">  </div>  
</body> 

Above code isn't working.. :(

is there any other way to send content from my website to other website??

any help would b greatly appreciated. -thanx

Ravi Dhoriya ツ
  • 4,435
  • 8
  • 37
  • 48
  • 1
    Does this question help? http://stackoverflow.com/questions/727183/can-jquery-ajax-call-external-webservice. You might also need to look into using a crossdomain.xml file, it's needed for Silverlight, I'm not sure about the javascript call. – AlignedDev Mar 23 '11 at 15:20
  • 1
    http://api.jquery.com/jQuery.getJSON/#jsonp is what you want to look into – mplungjan Mar 23 '11 at 15:36

3 Answers3

3

Your above example violates Same Origin Policy. You can circumvent this by doing the call through a proxy on the server side.

HurnsMobile
  • 4,341
  • 3
  • 27
  • 39
1

The answer is to use JSONP. It gets around the same-domain policy.

http://en.wikipedia.org/wiki/JSONP

Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152
  • Hi, thanks for the answer. 3 year ago when I faced this problem, I used answer given by James (because I wasn't in state of my mind to learn JSONP because of short of time to finish my project). Thanks for JSONP. I'l explore it to use in future :) – Ravi Dhoriya ツ May 29 '14 at 08:26
  • However there was a problem with my stackoverflow account so could not reply you in time. And created new account (now both accounts are merged with help of Stackoverflow team). Thanks a lot – Ravi Dhoriya ツ May 29 '14 at 08:29
1

You could simply have the full javascript widget code on your own server which your website owner friends could paste into their site with a simple

<script type="text/javascript" src="http://example.com/myjavacript.ashx"></script>

This widget would then create the form, input boxes, text information you want to display, etc., for carrying out the query, by using document.write("Some HTML");

However, website owners would probably prefer simply inserting an iframe from your site which contains the widget. This is more trustworthy; otherwise, you could always modify your javascript to make it grab user information from protected pages. Unless you need to be modifying the HTML on their pages, instead of inserting a simple widget, the iframe method is preferable, since it keeps users of your information better protected and creates a more trusting environment.

[ EDIT: Please note the answer below by HurnsMobile about same-origin policy - it is important. ]

James
  • 661
  • 7
  • 16