从javascript调用网址 [英] Call a url from javascript
问题描述
有没有办法调用网址并使用javascript获取响应?
我需要相当于ASP.NET:
Is there a way to call a url and get a response using javascript?
I need the equivalent of ASP.NET:
WebRequest req = HttpWebRequest.Create("http://someurl.com");
WebResponse webResponse = req.GetResponse();
我有一个外部网址,其中包含我需要的一些信息,我想从javascript中调用此网址解析响应以确定在我的应用程序中要执行的操作。
I have an external url that holds some information I need and I want to call this url from javascript and parse the response in order to determine what to do in my application.
推荐答案
如果网址在,则可以发出AJAX请求相同的域,例如,相同的主机不同的应用程序。如果是这样,我可能会使用像jQuery这样的框架,很可能是获得方法。
You can make an AJAX request if the url is in the same domain, e.g., same host different application. If so, I'd probably use a framework like jQuery, most likely the get method.
$.get('http://someurl.com',function(data,status) {
...parse the data...
},'html');
如果遇到跨域问题,那么最好的办法是创建服务器端操作,代理您的请求。使用AJAX向服务器请求,请求服务器请求并从外部主机返回响应。
If you run into cross domain issues, then your best bet is to create a server-side action that proxies the request for you. Do your request to your server using AJAX, have the server request and return the response from the external host.
感谢@ nickf,指出我的问题显而易见如果网址位于不同的域中,则为原始解决方案。
Thanks to@nickf, for pointing out the obvious problem with my original solution if the url is in a different domain.
这篇关于从javascript调用网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!