Home>

When the return value of the controller method is a simple type such as string, how to interact with json?

Use @requestbody

For example, the code is as follows:

@requestmapping (value="/ceshijson", produces="application/json;charset=utf-8")
@responsebody
public string ceshijson (@requestbody string channelid) throws ioexception {
 return channelid;

If the code is the case above,When sending json in the foreground, it should be written like this (there are many ways to write it,(It works)

function channel () {
   //Get the selected value first
   var channelid=$("#channelid option:selected"). val ();
   //to determine the link sent
   if (channelid == 2) {
   $.ajax ({
     url:"ceshijson",     type:"post",     datatype:"json",     contenttype:"application/json;charset=utf-8",     data:json.stringify ({"channelid":channelid}),     success:function (data) {
      alert (data.channelid);
     },     error:function (xmlhttprequest, textstatus, errorthrown) {
     alert ("error")
     alert (xmlhttprequest.status);
     alert (xmlhttprequest.readystate);
     alert (textstatus);
     }
   });
   }
  }

Special attention needs to be paid here:the previous chapter also emphasized,When @requestbody is used, it requires the string channelid to receive the data as a json string. That is, if data is written like this:data:{"channelid":channelid}, it is wrong.Because this is in the form of a json object.

If you don't want to use the json.stringify () function,Then manually string stitching yourself:

data:"{" channelid ":" + channelid + "}"

Also note here that channelid is double quoted,Cannot be written as single quotes,Because this is the json syntax rule.You changed to single quotes,That is

** Wrong writing

data:"{" channelid ":" + channelid + "}"

This form,Although it can be passed to the background,But undefined will appear in the background. That is, the key must be enclosed in double quotes.

Not using @requestbody

@requestmapping (value="/ceshijson", produces="application/json;charset=utf-8")
 @responsebody
 public string ceshijson (string channelid) throws ioexception {
  map<string, object>map=new hashmap<string, object>();
   map.put ("channelid", channelid);
   objectmapper mapper=new objectmapper ();
   channelid=mapper.writevalueasstring (map);
  return channelid;
 }

Foreground code

$.ajax ({
   url:"ceshijson",   type:"post",   datatype:"json",   //contenttype:"application/json;charset=utf-8",   data:"channelid =" + channelid,   success:function (data) {
    alert (data);
   },   error:function (xmlhttprequest, textstatus, errorthrown) {
     alert ("error")
     alert (xmlhttprequest.status);
     alert (xmlhttprequest.readystate);
     alert (textstatus);
    }
});

This method uses writevalueasstring in objectmapper to convert java objects to json strings.

Summary:this way,Actually it does n’t have much practical meaning,Because generally received data is not received this way.Just understand!

  • Previous Addition, deletion, modification and investigation of MyBatis entry + database field and entity field inconsistency
  • Next Detailed explanation of optional types returned by SpringMVC Controller