doushuangdui5419 2016-08-15 14:31
浏览 47
已采纳

如何使用AJAX创建跨域请求以及它们的工作原理

There is a web server on my LAN, at 10.xxx, on machine A.

I have an HTML page on machine B. This HTML page wants to make an AJAX request to the web server at 10.xxx. I made the page (with URL of file://abc.html) and tried it and got

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://XXXXXXXXXXX/ajax_info.txt. (Reason: CORS header 'Access-Control-Allow-Origin' missing)

My questions:

  1. Is there any way I can enable this in Mozilla?

  2. How can we make requests to Facebook, Twitter, and jQuery library servers via AJAX if cross-domain requests are prohibited?

  3. Can I add 'Access-Control-Allow-Origin' to vanilla AJAX requests?

This is probably a common question. I have seen lots of questions similar to this on Google, and SO as well.

  • 写回答

1条回答 默认 最新

  • dqssst0144 2016-08-15 14:33
    关注

    You put

    header('Access-Control-Allow-Origin: *');  
    

    on machine A

    If you want to be more secure you can add your domain in place of the * -

    http://www.foo.com for example

    header('Access-Control-Allow-Origin: http://www.foo.com');  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程