William
William

Reputation: 21

jQuery outputting string wrapped in quotes

I have this application where I'm going to update a database using jQuery and the HTTP PUT method.

There's some code before this, and some more after it. I'm having issues with the part where it has id: {...

The id variable is equal to: 9j6bddjg7fd6ee0df09j0989

I need it to end up looking something like this: "9j6bddjg7fd6ee0df09j0989"

The rest (path, http, etc.) work because they don't have quotes around them.

Is there a way that I can have the id surrounded by quotes right before it is sent off to /api/routes?

    password = $('#password-' + i).val();
        id = $('#id-' + i).html();

        jQuery.ajax({
        url: "/api/routes",
        type: "PUT",
        data: {
            "routes": [
                {
                  id : {
                  "path" : path,
                  "httpMethod": http,
                  "ensureAuthenticated": password,
                  "route": route
                  }
                }

Upvotes: 0

Views: 65

Answers (1)

mfreitas
mfreitas

Reputation: 2405

If you want the "id" key to be dynamic ( to have the value of your id variable ) you need to use a different notation:

var id = "9j6bddjg7fd6ee0df09j0989",
    newRoute = {},
    jsonData = {
        "routes": []
    };

newRoute[id] = {
    "path" : "path",
    "httpMethod": "http",
    "ensureAuthenticated": "passwd",
    "route": "route"
}

jsonData.routes.push(newRoute);

$.ajax({
  type: 'POST',
  dataType: 'json',
  url: '/echo/json/',
  data : { json: JSON.stringify( jsonData ) },
  success: function(data) {
      console.log("data > ", data);
  }  
});

Check this fiddle for a working copy and check your js console for the output.

Upvotes: 1

Related Questions