@
@
@Predaytor2019-11-20 19:15:04
JavaScript
@Predaytor, 2019-11-20 19:15:04

405 Method Not Allowed. Why is Ajax (Post) request not working?

function postAjax(url, data, success) {
  var params = typeof data == 'string' ? data : Object.keys(data).map(
    function (k) { return encodeURIComponent(k) + '=' + encodeURIComponent(data[k]) }
  ).join('&');

  var xhr = new XMLHttpRequest();
  xhr.open('POST', url);
  xhr.onreadystatechange = function () {
    if (xhr.readyState > 3 && xhr.status == 200) { success(xhr.responseText); }
  };
  xhr.setRequestHeader("Access-Control-Allow-Accept", "application/json");
  xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(params);
  return xhr;
}


postAjax('http://localhost:5500/temp.php', 'p1=1&p2=Hello+World', function (data) { console.log(data); });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Shohruh Shaimardonov, 2019-11-20
@joeberetta

Because the server turned it off. More precisely, he knows about this method, but does not want to execute it. Read about 405 http error

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question