3

In laravel 5.8 project I am trying to use JQuery AJAX function to send a get request to one of project routes url the status of response is 200 which means that the request executed successfully but every time the function return the current view HTML in response

I tried to remove all the code in my php function and return only a JSON response but still getting HTML I also tried to change data type to JSON or text and setting cache to false but the response still the same I even tried to comment all the function the route requesting and the same response always come out I also tried many other solutions offered here or on other sites but no way to solve it

Here is my controller function that the route requesting:

public function seen()
{
    $user = User::find(Auth::user()->id);

    $notifications = $user->notification;
    foreach($notifications as $notification){
        $notification->seen = 1;
        $notification->save();
    }

    return response()->json(['status' => 'seen']);
}

Here is the function route:

Route::get('/seen', 'CustomAuth\AuthController@seen')->middleware('authUser');

And finally the AJAX function:

    function seen(){
        let url = '/seen';
        $.ajax({
            type: 'GET',
            url: url,
            success: function (data) {
                console.log(data);
            },
            error: function (data) {
                console.log(data);
            }
        });
    }

Response I get when i set contenType to text:

Response Headers

I expect that I would get a JSON response that I am try to return within the function but I suppose that the request do not reach the function and the problem is within the ajax request

12
  • 1
    Try to add echo json_encode($data); in PHP and in jquery try Datatype:json Commented May 20, 2019 at 10:35
  • I had a similar issue, forcing headers (specifically content-type to txt) helped. Commented May 20, 2019 at 10:44
  • I am getting an object that contains response attributes {readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …} @NiketJoshi Commented May 20, 2019 at 10:44
  • @KareemSaeed can you show a print out of response and request headers? Commented May 20, 2019 at 10:46
  • You could also try returning plain json rather than response()->json();, I'm quite sure there's a helper function to turn any array into json. Commented May 20, 2019 at 11:01

3 Answers 3

2

Try removing the middleware from your response.

Sign up to request clarification or add additional context in comments.

10 Comments

Still getting HTML in response after trying your function. I do not think that the problem is related to the route if it is web or api as I used to do it the same way I am doing now to add a web route and it was working very well
@KareemSaeed How are you printing the data in the screenshot? Are you simply doing console.log(data)?
console log the response in AJAX success function success: function (data) { console.log(data); }
@KareemSaeed I know it's a wild shot, but could you replace 'data' with 'result'? - yes, I mean the parameter name.
Ok I changed the parameter to 'result' but still nothing changes
|
0

try this

function seen(){
    let url = '/seen';   $.ajax({
    url: "getusersallapp",
    type: 'POST',
    dataType: 'json',
    data: {
       '_token': $('input[name=_token]').val()
    },
    }).done(function (data) {
      console.log(result);
    })
    .fail(function (xhr, status, error) {
         console.log(error);
     });

Comments

0

As JCode suggested the problem was within the middleware added to the route.
As if case in the middleware returns false the middleware redirects to the current page so it returns the page HTML code in AJAX response

1 Comment

I've editted my answer so if anyone stumbles upon it knows what the cause might be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.