Today writes a JSON data to the background and then returns a JSON data type in the background. However, it is found that the value taken by the background through Request.getparamter ("") is null.
So write a simple Ajax request to troubleshoot the problem
Front Code:
$(document).ready(function(){ $("#ajax").click(function(){ var depart="depart"; $.ajax({ "/AjaxReponse", data :"depart="+depart, "post", "json", function(data){ alert(data); } }); });});
Background code:
String depart=request.getParameter("depart");
Symptom: Background fetch to a value of NULL. But when the Google Chrome debugging tool is debugged, the request already has the value sent
I. User method
The normal POST request (excluding AJAX requests) in the HTTP header is Content-type application/x-www-form-urlencoded, which can be request.getparameter in the Java background ( Name). However, through the native Ajax request, it is not possible to get the incoming parameters in the Java background through Request.getparameter (name).
But in fact, it can be seen that Content-type is already the form of application/x-www-form-urlencoded. So the user's method does not apply
http://m.blog.csdn.net/blog/eyebrother/36007145
Two. Encoding format
Since it is not a problem to see the request sent in the debugger, it is the encoding format problem. Add code in the background:
Request.setcharacterencoding ("Utf-8");
can solve this problem.
But I found the front desk written this type
data :{ "depart" : depart },
The background fetch is also null. So finally the JS file encoding format changed to UTF-8. Solve this problem
Three. Send JSON type data to the background
$ (document). Ready ( function(){$("#ajax"). Click ( function(){ varIsreceipt ="1";varadress ="2";varReason ="3";varProjectInfo = {"Adress": Isreceipt,"Ownerdept": {"Deptcode": adress},"Reason": Reason}; $.ajax ({URL:p ath+"/ajaxreponse", data: {"Depart":JSON. Stringify (ProjectInfo)}, type:"POST", DataType:"JSON", Success: function(data){alert (data); } }); });});
Background:
String depart=request.getParameter("depart");Gson gson = new GsonBuilder().create();Depart dep = gson.fromJson(depart), Depart.class);
The foreground converts the JSON type to a string type by using the Json.stringify () method.
The background is to use Google's Gson package and then convert the JSON type data (String) to the entity class data type.
four. JS Tool
Because the JS syntax is more complex, the compiler cannot check it. So JS code style, error is difficult to find. The two tools I use are recommended here.
1. Jstool
Can format JS code
2. JSLint
Check for syntax errors
The specific use is not detailed, I am downloading the two tools corresponding to the notepad++ plugin. It is very convenient to use in Notepad.
Five. Spit Groove
CSDN's new version of the blog editor is really good, but when writing a blog is not readily saved, open the other Csdn page, prompted Markdown Editor instance has been run, you must override the load. Then the whole blog is rewritten. I can't bear it!
Ajax POST Request Request.getparameter ("") value is null