Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

302 redirect when using Laravel's Form Request Validation #148

Closed
karlpatrickespiritu opened this issue Oct 28, 2016 · 14 comments
Closed

302 redirect when using Laravel's Form Request Validation #148

karlpatrickespiritu opened this issue Oct 28, 2016 · 14 comments

Comments

@karlpatrickespiritu
Copy link

karlpatrickespiritu commented Oct 28, 2016

Whenever I typehint a request validation in controller function like so:

/**
 * Store a newly created resource in storage.
*
 * @param StoreStaffEventLog|Request $request
* @return \Illuminate\Http\Response
 */
public function store(StoreStaffEventLog $request)
{
    if (!$this->eventStaffRepository->find($request->staff_event_id)) return $this->respondBadRequest();

    $this->eventStaffLogRepository->create([
         'staff_event_id' => $request->staff_event_id,
         'type' => $request->type,
    ]);

     return $this->respondCreated();
}

I'll get a 302 response redirect. See JS response below:

XMLHttpRequest cannot load http://company-site.com/api/1.0/staff/logs. Redirect from
'http://company-site.com/api/1.0/staff/logs' to 'http://localhost:8100/' has been blocked 
by CORS policy: Request requires preflight, which is disallowed to follow cross-origin redirect.

But if I replace the StoreStaffEventLog $request to Request $request. My request works fine and returns me a 201 Created status code.

@ibnujakaria
Copy link

i have the same problem with laravel 5.2 when using form request validation

@barryvdh
Copy link
Member

Closing this as part of an issue-cleanup. Please re-open if this is still an issue on the latest version.

@NoelDavies
Copy link

I have the same issue, running 5.5.28

@simplyrupin
Copy link

Yes, agree with @NoelDavies the issue has returned when using Laravel 5.5.28

@yunmoxue
Copy link

yunmoxue commented Feb 11, 2018

laravel 5.5
i put the the request header solve the problem.
'X-Requested-With': 'XMLHttpRequest'

laravel will return json when found this header.

actual, there are two method return json.
in file vendor/laravel/framework/src/Illuminate/Http/Concerns/InteractsWithContentTypes.php::42
public function expectsJson() { return ($this->ajax() && ! $this->pjax()) || $this->wantsJson(); }
the ajax judges the request header whether exists X-Requested-With and equal XMLHttpRequest

@S33G
Copy link

S33G commented Mar 11, 2019

Same issue persists with me today, "laravel/framework": "5.8.*"

@yunmoxue's suggestion to add a header resolved my issue

@lbrevity
Copy link

Hi
I have same issue .. I use form post not ajax call. so where i add X-Requested-With?

@amadoutidiane
Copy link

i need to know where put it @yunmoxue or @CharJ 'X-Requested-With': 'XMLHttpRequest'

@yunmoxue
Copy link

@lbrevity @amadoutidiane you can add request header when you use xhr.
there is api.
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader

@amadoutidiane
Copy link

@yunmoxue and @CharJ i'am new ate dev and i need you write all the code of ajax request and tell who i exactly put 'X-Requested-With': 'XMLHttpRequest' in the code thx for help

@S33G
Copy link

S33G commented Jun 24, 2019

An example of adding the header to a jQuery Ajax for @amadoutidiane.

Thought I'd share the solution for others here 🙂

$.ajax({
  type: "POST",
  beforeSend: function(request) {
    request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  }
});

@jacobsmith
Copy link

At least in Laravel 6.0, setting a request header of Accept: application/json will also trigger a JSON response (without needing to set the X-Requested-With header).

@jnnbnflr
Copy link

jnnbnflr commented Aug 5, 2022

This solves the problem for me:
Insert Accept: application/json in ajax header

$.ajax({
url:'{{route('fetchMunCit')}}' +'/'+id,
type:'GET',
dataType:'json',
Accept: application/json
success:function(response)

@maxcotech
Copy link

Adding Accept: application/json did not solve it for me

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests