Differing Response Options #123
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Hey there,
I'm adding PJAX to an application I've been working on in Laravel, but I'm having trouble creating custom responses for when an action is "rejected"
In this particular example, without PJAX added in, the application displays a toast notification when a user tries to delete a resource that is currently being edited by a different person.
At the moment, within the controller I have this response for it
Which responds with a JSON object with a key of flash (and it correctly returns this response).
However, with PJAX added in, it tries to load the content with the
loadUrlmethod and subsequently with theloadContentmethod. I'm wondering if a solution to this would be to add something toloadContentat the start which guards against JSON responses and simply returns that as part of the payload for the developer to utilize? Effectively making it into a normal AJAX request? Or is there a way to do this natively with PJAX?I've got the correct behaviour by adding the following code into the
this.requestmethod insideloadUrlwhile adding a specific trigger for JSON at the same time, though I suspect this is completely unneededIs this something to be considered to add to PJAX?
Why do you need to use Pjax for this? Why can't you make a simple AJAX request? Pjax isn't meant to be used with Json responses.
Because if the resource isn't being edited at the time, it should make a normal pjax request to the page instead (to be edited), it is basically decided server-side with which response should be sent
I forgot to add that also if someone goes to the link without using pjax that it responds with a redirect to the resource homescreen with a flash message of the same text
I don't think this is a common architecture. In my experience, APIs either return server-side rendered HTML or JSON, but not both.
I think it would make more sense for you to extend Pjax and implement your own code to parse the response.
What we can do to make that easier is to move the XHR callback to a separate prototype method of Pjax, to make it easier to override.
@robinnorth What do you think?
I'd be happy with a way to get to the request earlier in the call :), like an
onrequestfunction that fires before it does the rest of the request (i.e. above theif (html === falseline)Perhaps something on the lines of:
And it's not really working with an API per se
I agree that it's a bit unusual for the same URL route/API endpoint to return a different response type for the same request, dependent on some other condition (in this case, whether a resource is being edited or not). The way that you have it working without PJAX -- a redirect to another route which can then show a flash message -- is more in keeping with RESTful API best practices, as you're making use of HTTP status codes correctly.
PJAX already handles requests based on HTTP status and considers anything that returns a code other than 200 (OK) an error, which results in the
pjax:errorevent being triggered on the document element. @BehindTheMath, I wonder if actually we could just pass the request object to the error event to allow applications using PJAX to then use the request object to handle the error condition however they see fit. In @zeraphie's case, that would be to get therequest.responseText, parse it as JSON and do something with it to show the user a message.That's a good idea. Regardless whether it solves this issue, it would be a good idea to give the user access to the response so they can decide what to do in event of unexpected data.
That's pretty much what I was looking for :)
@zeraphie Where exactly does the error occur? Is it caught by this
catchblock?Ahhhh I think it was a thrown error but I'd need to be at work to double check
Ok. I'm just trying to determine where to put the error handling code.
If we implement my suggestion of relying on the user making use of HTTP status codes (e.g. returning a 302 redirect in @zeraphie's case), then this block should trigger the
pjax:errorevent that we'd want to pass the request object to.True. However, if it was a 200 response, but the body was malformed for some reason, it would not be caught there. It might be caught here, but I haven't tested that yet. Additionally, that doesn't trigger a
pjax:errorevent.@zeraphie Were you able to check?
Hey!
Sorry, I've been swamped with work lately but should be able to check shortly!
Yeah, I can confirm it is caught there