apache cors origin problem with laravel and angular

mmvthczy  于 2023-06-06  发布在  Apache
关注(0)|答案(1)|浏览(139)

我得到这个错误在我的浏览器当我试图使后或把或删除请求我试图编辑响应头在控制器和有中间件,但仍然得到错误在其中一个控制器它为我工作(获取,后,删除)请求,但仍然有问题的把请求然而这里是工作控制器的代码(只是后删除获取)

<?php

namespace App\Http\Controllers;

use App\Models\locations;
use Illuminate\Http\Request;

class locationController extends Controller
{
    public function store(Request $request){
        $data = $request->validate([
            'name'=>'required|min:3'
        ]);
        $response = [
            'returnCode'=>200,
            "errorMsg"=>"",
            "result"=>locations::create($data)
        ];
         return response()->json($response,200)->header('Access-Control-Allow-Origin', '*')
        ->header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE')
        ->header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
        
       }
       
public function delete($id){
        $location = locations::destroy($id);
      
        $response = [
            'returnCode'=>200,
            "errorMsg"=>"",
            "result"=>$location
        ];
        return response($response,200)->header('Access-Control-Allow-Origin', '*')
        ->header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE')
        ->header('Access-Control-Allow-Headers', 'Content-Type, Authorization');  
    }
     public function update(locations $location,Request $request){
        $data = $request->validate([
            'name'=>'required',
            
        ]);
        $location->update($data);
        $location->save();
        $response = [
            'returnCode'=>200,
            "errorMsg"=>"",
            "result"=>$location
        ];
        return response($response,200)->header('Access-Control-Allow-Origin', '*')
        ->header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE')
        ->header('Access-Control-Allow-Headers', 'Content-Type, Authorization');  
    }

    public function getById($id){
        $response = [
            'returnCode'=>200,
            "errorMsg"=>"",
            "result"=>locations::find($id)`your text`
        ];
        return response($response,200);
    }
    public function index(){
        $response = [
            'returnCode'=>200,
            "errorMsg"=>"",
            "result"=>locations::all()
        ];
        return response($response,200);
    }
    

}

这是同一个控制器的路由

Route::get('/location/getall',[locationController::class,'index']);
Route::get('/location/getbyid/{id}',[locationController::class,'getById']);
Route::post('/location/add',[locationController::class,'store']);
Route::put('/location/update/{location}',[locationController::class,'update']);
Route::delete('/location/delete/{id}',[locationController::class,'delete']);
Route::options('/location/update/{location}', function () {
    return response()->json(null, 200)
        ->header('Access-Control-Allow-Origin', '*')
        ->header('Access-Control-Allow-Methods', 'PUT,GET,DELETE,POST')
        ->header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
});

下面是控制器代码,它不适用于任何(put,delete,post)

namespace App\Http\Controllers;

use App\Models\Lands;
use App\Models\locations;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Spatie\QueryBuilder\QueryBuilder;

use function PHPSTORM_META\map;

class landController extends Controller
{
   public function store(Request $request){
    
    $data = $request->validate([
        'ownerName'=>'required',
        'size'=>'required',
        'price'=>'required',
        'phoneNumber'=>'required',
        'locations_id'=>'required',
        'aquariumNumber'=>'required',
        'partNumber'=>'required',
        'streetNumber'=>'required',
        'streetWide'=>'required'
    ]);
    
    $land = Lands::create($data);
    $response = [
        'returnCode'=>200,
        "errorMsg"=>"",
        "result"=>$land
    ];
    return response($response, 200)
        ->header('Access-Control-Allow-Origin', '*')
        ->header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE')
        ->header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
   
   }
//    public function search(Request $request){
//     //return locations::where('','LIKE',"%$request->name%")->get();
// }
    public function index(Request $request){
        $lands = lands::with(['locations'])->get();
      
        $response = [
            'returnCode'=>200,
            "errorMsg"=>"",
            "result"=>$lands
        ];
        return response($response,200);
    }
    public function getById($id){
        $lands = lands::with(['locations'])->find($id);
      
        $response = [
            'returnCode'=>200,
            "errorMsg"=>"",
            "result"=>$lands
        ];
        return response($response,200); 
    }
    
    public function handleCorsPreflight()
{
    return response(null,200)
        ->header('Access-Control-Allow-Origin', '*')
        ->header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE')
        ->header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
}
    
    public function delete($id){
        $lands = lands::destroy($id);
      
        $response = [
            'returnCode'=>200,
            "errorMsg"=>"",
            "result"=>$lands
        ];
        return response($response,200);  
    }
    public function update(Lands $land,Request $request){
        $data = $request->validate([
            'ownerName'=>'required',
            'size'=>'required',
            'price'=>'required',
            'phoneNumber'=>'required',
            'locations_id'=>'required',
            'aquariumNumber'=>'required',
            'partNumber'=>'required',
            'streetNumber'=>'required',
            'streetWide'=>'required'
        ]);
        $land->update($data);
        $land->save();
        $response = [
            'returnCode'=>200,
            "errorMsg"=>"",
            "result"=>$land
        ];
        return response($response,200);  
    }

这是同一个控制器的路由

`
`Route::options('/land/add', [landController::class,'handleCorsPreflight']);
Route::get('/land/getall', [landController::class,'index']);
Route::get('/land/getbyid/{id}', [landController::class,'getById']);
Route::post('/land/add', [landController::class,'store']);
Route::put('/land/update/{land}', [landController::class,'update']);
Route::delete('/land/delete/{id}', [landController::class,'delete']);``

最后这里是错误代码
CORS策略已阻止从源“https://www.example.com”访问位于“https://swift-serv.com/api/land/add/”的XMLHttpRequesthouse-land-asem.000webhostapp.com:对印前检查请求的响应未通过访问控制检查:预检请求不允许重定向。

***一些注意事项我使用hostinger为laravel和000 webhost为角

谢谢你的帮助,如果我写得太多,我很抱歉

cgfeq70w

cgfeq70w1#

我在使用angular和php时也遇到了同样的cors问题。我认为你的问题可以通过添加一个访问控制允许起源头到你的php文件来解决。您可以从允许一切开始,检查您的设置是否有效,然后自定义为尽可能少的访问。请尝试以下代码:

<?php
header('Access-Control-Allow-Origin: *');
namespace App\Http\Controllers;

use App\Models\locations;
use Illuminate\Http\Request;

class locationController extends Controller
{...}
?>

如果这工作,你可以取代你的需要的星星。希望这能帮上忙。你也可以看看下面的视频,这对我帮助很大:)https://www.youtube.com/watch?v=IAJpoH3-Ap8&ab_channel=StandaloneDeveloper

相关问题