简体   繁体   中英

How to get content from another website by javascript in asp.net?

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

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

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

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

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. ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM