且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Zend框架2 - 来自其他服务器的AJAX请求

更新时间:2023-02-23 15:15:49

由于Bodgan的回答说,这是一个浏览器的安全问题,而不是ZF2问题。一种流行的方式来解决它是改变访问控制 - 允许原产地您的域A的允许从域B的请求这和其他解决方案的讨论Mozilla开发者网络(MDN)页 HTTP访问控制(CORS)

As Bodgan's answer stated, this is a browser security issue rather than a ZF2 issue. One popular way to get around it is to change the ACCESS-CONTROL-ALLOW-ORIGIN of your domain A to allow requests from domain B. This and other solutions are discussed on the Mozilla Developer Network (MDN) page for HTTP access control (CORS).

基本上你需要告诉接收服务器(域A),它是好的,为资源的请求作出回应。您可以在的.htaccess 文件放置在域A下面的Web根目录做一些简单的示例code,表示为域A,它应该响应从所有域资源共享的要求: * 。该MDN给出的链接进入一个更深入的讨论跨域资源共享(CORS)。请记住,有安全隐患,而且在大多数情况下,你不希望从打开你的服务器的请求 * 的起源,而是由自己控制的特定主机

Basically you need to indicate to the receiving server (domain A) that it is okay to respond to requests for resources. You can do this within a .htaccess file placed in the web root of domain A. Below is some simple sample code that indicates to domain A that it should respond to resource sharing requests from all domains: *. The MDN article linked to above goes into a more in-depth discussion of "Cross-Origin Resource Sharing (CORS)". Keep in mind that there are security implications, and in most scenarios you do not want to open up your server to requests from * origins, but rather to a specific host controlled by yourself.

Options +FollowSymlinks
RewriteEngine on

Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"