1

I don't know what I did wrong and need help.

function loadDoy() {
    axios({
        method: 'post',
        headers: {

"Access-Control-Allow-Origin": "*",
            'Content-Type': 'application/json',
        },
        url: apiPostUrl,
        data: {
            user: "webuser",
            password: "m0nk3yb@rz",
            layout: "Main Menu"
        },
    })
    .then(function(response) {
        this.token = response.data.token;
        //if login token works then get records
        getRecords();
    })
    .catch(function(xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
    });
}
will b
  • 21
  • 1
  • 2
  • This is my error: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 405. – will b Jun 18 '18 at 21:37

1 Answers1

0

You can use 'crossDomain': true at your header of axios post, this is due to cors error. Your browser is making a Preflight Request, which uses the OPTIONS HTTP method. This is to check whether the server will allow the POST request – the 405 status code is sent in the response to the OPTIONS request, not your POST request

In this case, the following is causing the request to be preflighted:

if the Content-Type header has a value other than the following:

  1. application/x-www-form-urlencoded

  2. multipart/form-data

  3. text/plain

The value for the Content-Type header is set to application/json;charset=utf-8 by axios. Using text/plain;charset=utf-8 or text/plain fixes the problem: You may try using like below.

source: App Script sends 405 response when trying to send a POST request

    axios({
        method: 'post',
        headers: {    
            'crossDomain': true,
            //'Content-Type': 'application/json',
            'Content-Type': 'text/plain;charset=utf-8',
        },
        url: apiPostUrl,
        data: {
            user: "webuser",
            password: "m0nk3yb@rz",
            layout: "Main Menu"
        },
    })
    .then(function(response) {
        this.token = response.data.token;
        //if login token works then get records
        getRecords();
    })
    .catch(function(xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
    });
Jeeva
  • 1,245
  • 7
  • 12
  • 1
    it still isn't working! It says the "Access-Control-Allow-Origin" is set to null? How do I fix this? – will b Jun 20 '18 at 20:32
  • I have updated the answer, kindly refer this source for more info and let me know if any, https://stackoverflow.com/questions/44910180/app-script-sends-405-response-when-trying-to-send-a-post-request – Jeeva Jun 21 '18 at 07:50