如何将JSON字符串或对象从dojo xhr post调用传递给用Java实现的POST方法

时间:2014-05-12 14:45:58

标签: java ajax json rest dojo

我正在尝试使用javascript(dojo xhr)将JSON字符串传递给使用Apache Wink库实现的POST方法。

下面是我使用dojo / _base / xhr的javascript代码:

var data = JSON.stringify({"id" : "1", "status" : "Red", "title" : "Incident 1"});

xhr.post
(
    {
       headers: { 
          'Content-Type': 'application/json' 
       },
       handleAs : "json",
       content : data,
       load : function(data, ioArgs)
       {
           alert(data);
       },
       error : function(error, ioArgs)
       {
           alert(error);                            
       }
   }
);

下面是我在Java中的POST方法代码: -

@POST
@Path("saveIncidents")
@Consumes(MediaType.APPLICATION_JSON)
public void saveIncidents(String incidentData) 
{ 
    try {
         JSONObject jsonObject = new JSONObject(incidentData);
    } catch (JSONException e) {
         e.printStackTrace();
    }    
}

然而,我得到例外:

A JSONObject text must begin with '{' at character 1

以下是我在String变量'incidentData'中得到的内容:

0=%7B&1=%22&2=i&3=d&4=%22&5=%3A&6=%22&7=1&8=%22&9=%2C&10=%22&11=i&12=n&13=c&14=i&15=d&16=e&17=n&18=t&19=S&20=t&21=a&22=t&23=u&24=s&25=%22&26=%3A&27=%22&28=R&29=e&30=d&31=%22&32=%2C&33=%22&34=i&35=n&36=c&37=i&38=d&39=e&40=n&41=t&42=T&43=i&44=t&45=l&46=e&47=%22&48=%3A&49=%22&50=I&51=n&52=c&53=i&54=d&55=e&56=n&57=t&58=%20&59=1&60=%22&61=%7D

有谁知道如何去做?在此先感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

你确定如何使用stringify吗?这是我从msdn。

获得的一个例子
var contact = new Object(); 
contact.firstname = "Jesper";
contact.surname = "Aaberg";
contact.phone = ["555-0100", "555-0120"];

var jsonText = JSON.stringify(contact);
document.write(jsonText);

答案 1 :(得分:0)

现在我已经解决了这个问题 -

@POST
@Path("saveIncidents")
@Consumes(MediaType.APPLICATION_JSON)
public void saveIncidents(String encodedIncidentData) {

    JSONObject jsonObject = null;
    String incidentData = "";
    StringTokenizer mainTokenizer = new StringTokenizer(encodedIncidentData, "&");

    while(mainTokenizer.hasMoreElements()) {
        String token = mainTokenizer.nextElement().toString();
        StringTokenizer innerTokenizer = new StringTokenizer(token, "=");

        String character = null;
        while(innerTokenizer.hasMoreElements()) {
            character = innerTokenizer.nextElement().toString();
        }

        if(character.equals("%2C")){
            character = ",";
        }
        else if(character.equals("%22")){
            character = "\"";
        }
        else if(character.equals("%7B")){
            character = "{";
        }
        else if(character.equals("%7D")){
            character = "}";
        }
        else if(character.equals("%3A")){
            character = ":";
        }
        else if(character.equals("%5D")){
            character = "]";
        }
        else if(character.equals("%5B")){
            character = "[";
        }
        else if(character.equals("%20")){
            character = " ";
        }

        incidentData += character;

    }

    try {
        jsonObject = new JSONObject(incidentData);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

不过,如果有任何有效的解决方案,请告诉我。感谢。