Dojo XhrPost为服务器上发布的json对象设置名称

时间:2014-04-16 15:41:50

标签: ajax json dojo http-headers

我有 Dojo ajax 请求,我将数据发布为 json ,但数据未以 json 格式到达服务器。我也无法在控制台的浏览器 Net 选项中看到 JSON 标签。我需要服务器上的 json 格式的数据。

Ajax请求

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

服务器详细信息的屏幕截图

我希望下面的JSON数据以下面的格式显示,名称为MyData。这种格式在服务器上是如何实现的?

JSON

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

来源

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

enter image description here

2 个答案:

答案 0 :(得分:1)

实际上url:''在你的Ajax调用中是空的。请提供行动网址,

假设

url: "AddTrackServlet"

答案 1 :(得分:0)

在玩变量后,我看到它可以被声明

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