简体   繁体   English

如何使用ajax或javascript进行跨域请求?

[英]How to make cross domain request using ajax or javascript?

I have some Pre built script that makes some ajax request to one website. 我有一些预先构建的脚本,该脚本向一个网站提出了一些ajax请求。 And it's working fine. 而且工作正常。 But I am not able to make request to there some domains. 但是我无法向其中的某些域提出请求。

My working Process:- 我的工作流程:

1) I have pre built script on http://www.Test.com/a.js now i am calling this js in this domain http://www.test1.com by pasting the code to URL and it's working fine. 1)我已经在http://www.Test.com/a.js上预先构建了脚本,现在我通过将代码粘贴到URL在此域http://www.test1.com中将此js调用,并且可以正常工作。

2) Now When I want to call one page from This http://hello.test1.com/as.php but it's showing some errors. 2)现在,当我想从This http://hello.test1.com/as.php调用一个页面时,却显示了一些错误。 How could i make request to subdomain page from main domain page. 我如何从主域名页面向子域名页面发出请求。

Please let me know how can i do this. 请让我知道我该怎么做。

Thanks in advance. 提前致谢。

Your various options for cross-domain requests are: 跨域请求的各种选项是:

  • The new Cross-Origin Resource Sharing (CORS) standard, if the browsers your users use support it and the server has support for it. 如果您的用户使用的浏览器支持新的跨域资源共享 (CORS)标准,并且服务器支持该标准。 Note that a lot of general web users still have browsers that don't support it. 请注意,许多普通的Web用户仍然拥有不支持它的浏览器。
  • JSON-P ("JSON with padding"), which is basically a dynamically-added script element JSON-P (“带填充的JSON”),基本上是动态添加的script元素
  • Using YQL (Yahoo Query Language) as a cross-domain proxy 使用YQL (雅虎查询语言)作为跨域代理

There are several work arounds for cross domain scripting restrictions. 跨域脚本限制有几种解决方法。 easyXDM seems to do what you want. easyXDM似乎可以满足您的要求。

You can use php curl to do the cross domain request using ajax. 您可以使用php curl使用ajax进行跨域请求。 An ajax request will call php curl page which in turn will call cross domain with url specified in php curl page and return back the response . 一个ajax请求将调用php curl页面,该页面随后将调用php curl页面中指定的url的跨域并返回响应。

I have implemented same and it works fine. 我已经实现了相同,并且效果很好。 Hope this helps Thanks. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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