Dojo XhrPost set name for posted json object on server

本小妞迷上赌 提交于 2019-12-11 19:01:54

问题


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"}]}


回答1:


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

for suppose

url: "AddTrackServlet"



回答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);


来源:https://stackoverflow.com/questions/23114089/dojo-xhrpost-set-name-for-posted-json-object-on-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!