7

I am working on a Laravel 5.6 project which is stored on a VPS (we call it "production", despite there is no such created environment).

We also combined Plesk & Github to deploy the web app from our local environments to the server manually.

The issue is when I load some data from the APIs they return error 405 Method not allowed (GET)... but they actually are registered as POST in the app.js and in routes/api.php.

And the best thing is that in my local environment they work perfectly.

Here some information:

The server:

  • Ubuntu Server 14.04
  • Apache / MySQL
  • PHP 7.2.5

My computer:

  • Windows 10 with XAMPP
  • Apache / MySQL
  • PHP 7.2.2

The Developer tool in every browser:

Request Method: GET
Status Code: 405 Method Not Allowed

And here is the code within the app.js:

loadCountries: function loadCountries(total) {
    axios.post('/api/properties/countries/').then(function (response) {
        app.countries = response.data;
    });

    total = total <= this.countries.length ? total : this.countries.length;

    if (total) {
        var newArr = [];
        for (i = 0; i < total; i++) {
            newArr.push(this.countries[i]);
        }
        this.countries = newArr;
    }
},

Note: If I edit the same request in the developer tool and send it again but as a POST request it returns me everything ok, so the API seems to work fine on POST request.

5
  • This answer may be relevant to your issue stackoverflow.com/questions/46611275/… Commented May 22, 2018 at 2:38
  • I couldn't find that answer... It solved my issue. Please answer. and Thank you Commented May 22, 2018 at 2:51
  • @Maramal: If it's working in postman then try to remove / from request. Try it like: '/api/properties/countries' because i think it's tried to find '/api/properties/countries/' and it's not defined in your routes! Commented May 22, 2018 at 5:13
  • @kozak please post as an answer Commented May 22, 2018 at 10:29
  • @Maramal, NP glad it worked. Commented May 22, 2018 at 17:10

2 Answers 2

4

Try to remove the trailing slash in your url.

Such as,

/api/properties/countries

Replacing that line in your original app.js would yeild this,

loadCountries: function loadCountries(total) {
axios.post('/api/properties/countries').then(function (response) {
    app.countries = response.data;
});

total = total <= this.countries.length ? total : this.countries.length;

if (total) {
    var newArr = [];
    for (i = 0; i < total; i++) {
        newArr.push(this.countries[i]);
    }
    this.countries = newArr;
}

},

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

Comments

0

i faced this issue and i solve it by make computed attribute with full url like that

computed: {
    sendAnswersUrl: function () {
        return window.location.protocol + "//" + window.location.hostname, +  "/api/properties/countries";
     }
  }

then when post using axios

axios.post(this.sendAnswersUrl, {
          group: this.id,
        })
        .then(data => {})
        .catch(() => {});

Comments

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.