CORS JSON php 请求的资源上不存在“Access-Control-Allow-Origin”标头

use*_*222 2 php json angularjs

我根本不知道如何解决这个问题,我已经坐在那里好几个小时了,什么也没发生。 错误

从我的角度来看,我像这样使用 http.post :

function PostReview(JSONObject) {
    if(JSONObject!=null){
         $http({ 
             url: 'http://localhost:8000/creation',
             method: "POST",
             data: JSONObject,
             headers: { "Content-Type":  "application/json"} });

         }
    }
Run Code Online (Sandbox Code Playgroud)

在 php 中我尝试这样得到它:

public function created()
{
 $json = file_get_contents('php://input');
 $request = json_decode($json);
 var_dump($request);
// $email = $request->email;
//return response()->json($request);
}
Run Code Online (Sandbox Code Playgroud)

我还有 Cors 类,我在其中处理 OPTIONS 预检问题。我尝试在那里添加标题,但没有运气:

    class CorsMiddleware {
    protected $settings = [
                'origin' => '*',    // Wide Open!
                'allowMethods' => 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS',
                'allowHeaders' => 'Content-Type, Origin'];
    protected function setOrigin($req, $rsp)
    {
        $origin = $this->settings['origin'];
        if (is_callable($origin))
        {
            // Call origin callback with request origin
            $origin = call_user_func($origin, $req->header("Origin"));
        }
        $rsp->header('Access-Control-Allow-Origin', $origin);
    }

    protected function setExposeHeaders($req, $rsp)
    {
        if (isset($this->settings->exposeHeaders))
        {
            $exposeHeaders = $this->settings->exposeHeaders;

            if (is_array($exposeHeaders))
            $exposeHeaders = implode(", ", $exposeHeaders);

            $rsp->header('Access-Control-Expose-Headers', $exposeHeaders);
        }
    }
Run Code Online (Sandbox Code Playgroud)
    protected function setMaxAge($req, $rsp)
    {
        if (isset($this->settings['maxAge']))
        $rsp->header('Access-Control-Max-Age', $this->settings['maxAge']);
    }

    protected function setAllowCredentials($req, $rsp)
    {
        if (isset($this->settings['allowCredentials']) && $this->settings['allowCredentials'] === True)
        $rsp->header('Access-Control-Allow-Credentials', 'true');
    }

    protected function setAllowMethods($req, $rsp)
    {
        if (isset($this->settings['allowMethods']))
        {
            $allowMethods = $this->settings['allowMethods'];

            if (is_array($allowMethods))
            $allowMethods = implode(", ", $allowMethods);

            $rsp->header('Access-Control-Allow-Methods', $allowMethods);
        }
    }
Run Code Online (Sandbox Code Playgroud)
    protected function setAllowHeaders($req, $rsp)
    {
        if (isset($this->settings['allowHeaders']))
        {
            $allowHeaders = $this->settings['allowHeaders'];

            if (is_array($allowHeaders))
            $allowHeaders = implode(", ", $allowHeaders);

        }
        else // Otherwise, use request headers
        {
            $allowHeaders = $req->header("Access-Control-Request-Headers");
        }

        if (isset($allowHeaders))
        $rsp->header('Access-Control-Allow-Headers', $allowHeaders);

    }

    protected function setCorsHeaders($req, $rsp)
    {
        // http://www.html5rocks.com/static/images/cors_server_flowchart.png
        // Pre-flight
        if ($req->isMethod('OPTIONS'))
        {
            $this->setOrigin($req, $rsp);
            $this->setMaxAge($req, $rsp);
            $this->setAllowCredentials($req, $rsp);
            $this->setAllowMethods($req, $rsp);
            $this->setAllowHeaders($req, $rsp);
        }
        else
        {
            $this->setOrigin($req, $rsp);
            $this->setExposeHeaders($req, $rsp);
            $this->setAllowCredentials($req, $rsp);
        }
    }
Run Code Online (Sandbox Code Playgroud)
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next) {

        if ($request->isMethod('OPTIONS')) {
            $response = new Response("", 200);
        }
        else {
            $response = $next($request);
        }

        $this->setCorsHeaders($request, $response);

        return $response;
    }

}
Run Code Online (Sandbox Code Playgroud)

有谁知道如何解决它?我尝试了多个例子,但没有一个对我有用。


我想我在将角度帖子方法更改为以下内容后得到了修复:

function PostReview(JSONObject) {
    if(JSONObject!=null){
         $http({ 
             url: 'http://localhost:8000/creation',
             method: "POST",
             data: JSONObject,
             headers: { "Content-Type": "application/x-www-form-urlencoded;charset=utf-8;", "Accept": "application/json"} });

         }
    }
Run Code Online (Sandbox Code Playgroud)

现在我没有收到错误,方法是 post,但邮递员是空的。在此输入图像描述

Ell*_* B. 5

您很可能没有正确设置响应标头。您的屏幕截图表明回复中的标头设置正确OPTIONS,但回复的标头可能不同(并且不足)POST

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Content-Type, X-Requested-With');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT');
header('Access-Control-Max-Age: 600');
Run Code Online (Sandbox Code Playgroud)

header()必须在发送任何实际输出之前调用,无论是通过正常的 HTML 标签、文件中的空行还是从 PHP。

但是,从 PHP 代码可以明显看出,您正在 API 框架的范围内工作,并且强制设置标头header()可能不是最佳实践。