Pergunta

I have a Dojo ajax request and i am posting the data as json however the data is not reaching the server in json format. I am also not able to see the JSON tab in the browsers Net option of the console. I need the data in json format on the server.

Ajax Request

 var someData = [{id:"1",age:"44",name:"John"},
                           { id:"2",age:"25",name:"Doe"},
                           { id:"3",age:"30",name:"Alice"}];

    function SendData() {

   var xhrArgs = {
        url:'processData',
        postData: someData,
         handleAs: "json",
         headers: { "Content-Type": "application/json", "Accept": "application/json" },

        load:function(data){
            console.log('data was posted');
        },
        error:function(error){
            console.log(error);
        }
    };
    dojo.xhrPost(xhrArgs);

Screenshot of server details

I would like the JSON data under to appear in the following format under with the name MyData. How is this format possible on the server?

JSON

MyData              [Object{id:"1",age:"44",name:"John"},
                     Object{ id:"2",age:"25",name:"Doe"},
                     Object{ id:"3",age:"30",name:"Alice"}]

SOURCE

{"MyData":[{id:"1",age:"44",name:"John"},
               { id:"2",age:"25",name:"Doe"},
               { id:"3",age:"30",name:"Alice"}]}

enter image description here

Foi útil?

Solução 2

After playing around with the variable i saw it could be declared

var someData = [{id:"1",age:"44",name:"John"},
                           { id:"2",age:"25",name:"Doe"},
                           { id:"3",age:"30",name:"Alice"}];
var formData = {MyData:someData}
    function SendData() {

   var xhrArgs = {
        url:'processData',
        postData: dojo.toJson(formData),
         handleAs: "json",
         headers: { "Content-Type": "application/json", "Accept": "application/json" },

        load:function(data){
            console.log('data was posted');
        },
        error:function(error){
            console.log(error);
        }
    };
    dojo.xhrPost(xhrArgs);

Outras dicas

Actually url:'' is empty in your Ajax call. Please provide action url,

for suppose

url: "AddTrackServlet"
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top