0

I am able to create a database using futon or curl command but I am trying to create a database by getting the database name from the user. I had a textbox and a button when user types the database name and on clicking create db i am calling a ajax function to access the couch db. I am dont really know how to create a database on button click. I could get example like task list to insert edit and update but I am not able to create a database using ajax. I tried with this

$(document).ready(function(){
            $("#adddb").click(function(){
                var dbname = $("#dbname").val();
                 $.ajax({
                type: "PUT",
                url: DATABASE,
                contentType: "application/json",
                data: dbname
             });
            });
        });
<input type="text" id="dbname" placeholder="Database Name"> 
    <input type="button" id="adddb" value="Add Db">

I am getting this error.

PUT http://127.0.0.1:5984/ 405 (Method Not Allowed)

Is this a way to create a db or I am wrong. Can someone help me?

2 Answers 2

1

As the Doc suggests:

Your Request need not contain anything in the body, the request URL should itself contain the dbName

var dbName = $("#dbname").val();
$.ajax({
  type: "PUT",
  url: DATABASE_URL+"/"+ dbName
});

Also the dbName should make sure it follows these rules:

  • Name must begin with a lowercase letter (a-z)
  • Lowercase characters (a-z)
  • Digits (0-9)
  • Any of the characters _, $, (, ), +, -, and /.

Or follow this RegExp ^[a-z][a-z0-9_$()+/-]*$.

0

I was keep trying this following works fine for me.

 $("#adddb").click(function(){
                 dbname = $("#dbname").val();
                 $.ajax({
                type: "PUT",
                url: DATABASE+"/"+dbname,
                contentType: "application/json",
                data: dbname
             });

PUT request helps to create a database as it is in curl PUT command

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.