开发者

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

i'm creating a web application in as开发者_如何转开发p.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.


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 answer is to use JSONP. It gets around the same-domain policy.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜