Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Create JSON Hash Array for jQuery AJAX

I am desperately trying to manually create a JSON-style array in Javascript to send over the network via jQuery's AJAX method.

var fieldsobj = {fields:[]}
$(".fact_field", fact).each(function(index, field){
    var index  = $(field).attr("data-index");
    var name  = $(".fact_field_label", field).text().trim();
    var value  = $(".fact_field_value", field).text().trim();
    fieldsobj["fields"].push({index:index, name:name, value:value});
});
//...
$.ajax({
    type: 'PUT',
    url: url,
    data: fieldsobj,
    success: function(data){...
    },
    complete: function(){...
    }
});

What I want is the following:

{fields => [{index:0, name:1, value:2},{...},{...}]}

What I get is this:

 {"fields"=>{"0"=>{...}, "1"=>{..}, "2"=>{...}, "3"=>{...}}

What am I doing wrong?

like image 757
Denny Avatar asked Feb 18 '11 05:02

Denny


People also ask

How to convert array in jQuery to JSON format?

As Jquery is a library of JavaScript, the array in Jquery is similar to the array in JavaScript and therefore JavaScript provides different techniques to do such conversion to JSON format. There are two different methods of converting the given JavaScript object that means Jquery array into JSON format. They are: 1. assign(target, sources)

What is an arrays in JSON?

Arrays as JSON Objects. Example. [ "Ford", "BMW", "Fiat" ] Arrays in JSON are almost the same as arrays in JavaScript. In JSON, array values must be of type string, number, object, array, boolean or null. In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined.

How to create a dynamic JSON array in JavaScript?

This is one of most common scenarios and you’ll see two ways of creating JSON array dynamically. The first method will use for loop for creating JSON array from an input array. The second method will make use of the JavaScript reduce method to create dynamic JSON array. How JavaScript Variables are Stored in Memory? What Does ...

How do I use JSON with JavaScript?

A common use of JSON is to read data from a web server, and display the data in a web page. For simplicity, this can be demonstrated using a string as input. Then, use the JavaScript built-in function JSON.parse () to convert the string into a JavaScript object:


1 Answers

When you pass an object as the data property, jQuery will pass it as url-encoded form parameters (e.g. foo=bar&moo=too) in the body. I think what you want is to pass JSON through the body.

Grab the json2.js written by uncle Crockford and use JSON.stringify (that library provides the functionality for browsers that still don't support it):

$.ajax({
    type: 'PUT',
    url: url,
    data: JSON.stringify(fieldsobj),
    contentType: "application/json",
    success: function(data){...
    },
    complete: function(){...
    }
});

And don't forget to set the contentType property! On the PHP side, you can use json_decode to decode the raw body content:

$fieldsobj = json_decode(@file_get_contents('php://input'));
like image 146
Ates Goral Avatar answered Oct 17 '22 19:10

Ates Goral