user1953977
user1953977

Reputation: 163

XMLHttpRequest server responded with a status of 405 (Method Not Allowed)

I am using this HTTP POST Request code to send a json data to Couchbase bucket/document. I don't know exactly why it is throwing error as "XMLHttpRequest send Failed to load resource: the server responded with a status of 405 (Method Not Allowed)"

When I tested this, it is working fine through Postman.

var params = {
    "EmpID":"567453",
    "EmpAddress": "XX",
    "EmpAccount": "89723",
    "EmpName": "user1953977",
    "EmpType": "DEV"
}
var xhr = new XMLHttpRequest();
var url = "http://xxxx:PORT/xxxx/xxx/xxx/docs/docs/firstdoc/"; // Couchbase document url
xhr.open("POST", url, true);

xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Accept", "application/json" );
xhr.setRequestHeader("Authorization", "Basic XXXXXXXXXXXXXXXXXX");
xhr.setRequestHeader("Cache-Control", "no-cache");

var data = JSON.stringify(params);
xhr.send(data)

Can someone advise me where is the issue? Is there way a to fix?

Upvotes: 1

Views: 2988

Answers (1)

Matthew Groves
Matthew Groves

Reputation: 26151

I'm pretty sure that what you're doing is making POST requests to Couchbase Server from a browser. The 405 error you're getting is likely due to CORS. CORS cannot be turned off in Couchbase Server (unless you fork and compile yourself, I guess). Couchbase Server is not designed to be accessed directly from the browser like this.

Some options:

  1. Couchbase Server sits behind an API and you would use the SDK of your choice (node, Java, .NET, whatever) to interact with Couchbase there.

  2. Use Sync Gateway in front of Couchbase, which has an API that allows your to enable CORS (see Sync Gateway Public REST API documentation).

  3. Turn off same-origin policy in your browser (like in Chrome), but this is not typically practical for most applications, as you would need every user to turn off CORS.

Upvotes: 1

Related Questions