0

On the same Laravel project, I have an API at localhost:8000/api and a website at localhost:8000/admin. When I send the following request from the website, which is supposed to return a list of partners in JSON, it returns a status 0:

let token = "e9Klj0...";
$.ajax({
    url: "localhost:8000/api/partners",
    headers: {
        "Authorization": "Bearer "+token,
        "Accept": "application/json",
    },
    success: function(res) {
        console.log(res);
    },
    error: function(xhr) {
        console.log(xhr.status);
    }
});

According to the answers of this question, the different cases of status 0 are:

  • unreachable URL: I don't think the problem comes from there, I can access localhost:8000/api/partners via Postman.
  • intercepted request by the ad blocker: I've disabled uBlock Origin, but it still won't work.
  • interrupted request: I don't send any other request than that of AJAX.
  • cross-site scripting: I think that's the most likely case but my API and my website run on the same webserver, and AJAX handles internal requests without dealing with the CORS.

How could I fix this problem?

Thank you for your help.

Alihossein shahabi
  • 3,098
  • 2
  • 19
  • 42
JacopoStanchi
  • 1,336
  • 2
  • 15
  • 46
  • Chances are the problem is in your controller function that handles that route, so post that. – Luke Ramsden May 16 '18 at 14:52
  • You can always check the laravel log. It probably contains some more information, like route mismatches or other errors. – Loek May 16 '18 at 14:53
  • @Loek Where can I find it? – JacopoStanchi May 16 '18 at 14:56
  • `projectroot/storage/log/laravel.log` iirc – Loek May 16 '18 at 14:58
  • @LukeRamsden No matter the code of the `index()` function of `PartnerController` (I could very well put `return response()->json(null,200);`) nothing changes. – JacopoStanchi May 16 '18 at 15:00
  • 1
    @Loek The last entry of the log is from several hours ago, I don't think there is anything of importance in it. Thank you for the tip though. – JacopoStanchi May 16 '18 at 15:06
  • Go in your server and make sure it is receiving the request. Is it receiving it? If so, you are not responding properly. If not, your server is not receiving it (obviously), and might be a middleware/route/ui issue. – Lansana Camara May 16 '18 at 15:35
  • How could I do that? – JacopoStanchi May 16 '18 at 15:46
  • You have `/api/partners` defined in your server, right? Go to the controller that is defined for that route, and just make sure it is being hit. Do a `var_dump('foo')` or something like that. – Lansana Camara May 16 '18 at 15:52
  • url: "/api/partners" try this – Winston Fale May 16 '18 at 18:42
  • Is the request type `get` ? or `post`? Since you are sending the token, I assume it's `post` in that case you better specify as `post` and send a dummy `data` which then in your server check for it – Dhiraj May 16 '18 at 20:52

0 Answers0