Laravel dusk assert json response - laravel

I have a form where the page does not redirects to another page when the submission is successful. The submission is done using an ajax HTTP POST call, server responds with HTTP status code as 200, 422, 500 depending on the conditions.
I want to assert if the response from the ajax call is 200. How do I do this?

Related

Postman GET returning HTML and status 200

I am trying to call a Postman GET endpoint, and it is returning HTML and status 200, instead of a valid JSON response and status 200. It looks like it is not hitting the backend at all. I have a separate POST endpoint, which works fine and returns me a valid JSON response.
Could this be due to authorization headers? I am using the same authorization for both.
I managed to resolve the issue. I had to add a cookie header, which I got from the browser since I was able to call the service from the browser and I had authenticated on browser level.

Laravel AJAX call response is redirect to login page

I'm using AJAX. I call the server which calls an API and gets its response. I try to return the response in JSON to the client. But my Laravel has Middleware that checks token is valid. And when it's invalid, it'll redirect to login page. So the response I return is not JSON from API call but is the HTML of the redirected login page. I don't know what to do about this. One, the response is not the expected JSON and two, how do I keep the mechanism of redirecting to login page when the situation is an AJAX call and not the usual going from one page to the next.

Laravel 5.3 Returns 302 with Request Validation - Ajax Request

I am having trouble with an ajax request to a Laravel application, specifically making a POST request to an authentication controller. I'm sending a post request with SuperAgent to a controller that uses a Request class to validate the input. The request carries a password and a username. When I inspect the console I'm getting back a GET 200 OK and a POST 302 Not Found. I tried debugging the application routes but nothing seemed to work.
Turns out it was something very simple. Having used jquery for a long time to make ajax request, I overlooked a very important header. The 'Accept', 'application/json' Header. Debugging the Request validation, I noticed that Laravel's expectsJson method was returning false, so all I had to do was add said header to the SuperAgent request object.

laravel validation during ajax request

Please somebody explain this info from Laravel docs "When using the validate method during an AJAX request, Laravel will not generate a redirect response. Instead, Laravel generates a JSON response containing all of the validation errors. This JSON response will be sent with a 422 HTTP status code."
What does "during an AJAX request" exactly mean here?
If I'm set like this:
User submits input from a form in the view.
The route calls the method for post in the controller
The request is validated in the controller
case 1) Request passes validation and input is stored in the DB -> Response is returned as JSON for a script that updates the view on the fly.
case 2) Request doesn't pass validation, what is returned here? I think a redirect and if not how can you check if validation has failed to return a JSON instead?
And is this scenario similar to what the docs talk about? If not then what?
The response to an AJAX call in this case is a JSON string
Case 1: as you said
Case 2: A JSON String with the error messages. Look into your Chrome dev tools into the Network tab, there you'll see the exact responses.
When handling a validation failure, laravel automatically generates an error response. Normally this will redirect the user back to their form, but in the case of an AJAX request it will instead return a JSON response with the details of what failed validation.
Laravel relies on the symfony request object to detect AJAX requests, specifically this line:
return 'XMLHttpRequest' == $this->headers->get('X-Requested-With');
If you are encountering issues with your requests not being treated as AJAX, make sure your client is properly setting the X-Requested-With header

302 from an AJAX request

I send an AJAX request which returns a 302 (I see this using fiddler) but in firebug I never see the 302. I just see a 500.
Same thing happens with IE.
If an AJAX request returns a 302 do browsers swallow it?
Thanks
When performing an AJAX POST in my web application, I found that my browser was registering a 302 Found, followed by a 200 OK.
However, the JavaScript debugger showed the response as being a 200 OK, with the HTML content to be shown on the redirection page.
This demonstrates that the redirection is being handled internally by JavaScript. In this case, you could replace the page's content with that returned by the server's response.
document.open();
document.write(xhr.responseText);
document.close();
See this question for more details.

Resources