mercredi 20 janvier 2016

Laravel v5.1.28: 404 on AJAX?

I already searched here on SO and found out that most other users simply had a typo or something similar. But this does not seem to be the case for me. In fact, it's even weirder.

I have a viewer for conversations, which holds messages between users. When I visit the page normally, it works. but via AJAX, it throws a 404 - not an error.

Request path: /user/pm/convo/9

Running ->getPath() on it, returns: user/pm/convo/{one?}/{two?}/{three?}/{four?}/{five?}

The array of verbs it's responding to: GET,HEAD,POST,PUT,PATCH,DELETE

Controller:

public function anyConvo($conv_id) {
    $errors = [];
    $user = Auth::user();
    $convo = Conversation::findOrFail($conv_id);
    $msg = new Message();
    if(Request::has("pmReply")) {
        $to_conv_id = Request::input("pmReply.conv_id");
        $is_member = $user->conversationMemberships()->contains($to_conv_id);
        if(!$is_member) {
            $errors["Validation"][] = "There was an error during transmission. Please try again.";
        } else {
            if($to_conv_id !== $conv_id) {
                $errors["Validation"][] = "You tried to reply to an non-existant conversation.";
            } else {
                $msg->conv_id = $to_conv_id;
                $msg->from_id = User::me()->id;
                $msg->body = Request::input("pmReply.body");
                if(!$msg->save()) {
                    $errors = array_merge_recursive($errors, $msg->getErrors());
                }
            }
        }
    }
    // Find and backtrack all the messages, the laraway.
    $messages = Message::where("conv_id", $conv_id)
                       ->orderBy("id","DESC")
                       ->get();

    if(Request::ajax()) {
        // This came from the /user/pm/box page, most likely.
        // For this case, we need to simplify things a little...
        $flatMessages = [];
        foreach($messages as $message) {
            $flatMessages[] = [
                "from"      => $message->sender->username,
                "body"      => $message->body,
                "is_read"   => $user->hasReadMessage($message)
            ];
        }
        $resp = response()->toJson([
            "status" => "OK",
            "messages" => $flatMessages
        ]);
        \BIRD3\Backend\Log::info($resp);
        return $resp;
    } else {
        return $this->render("User::pm.convo",[
            "messages"=>$messages,
            "convo"=>$convo,
            "newMsg"=>$msg,
            "errors"=>$errors
        ]);
    }
}

Issuing a normal GET request without X-Requested-With: XMLHttpRequest resolves in me actually getting the page, but in it's HTML form. When using AJAX however, by also sending the X-Requested-With header, I get a 404, and the controller does not even seem to get hit at all. If it matters, I am using uxhr for issuing AJAX requests ( https://npmjs.org/uxhr ).

I am really lost and could use a poke into the right direction... Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire