Ajax post request. getParameter ("") value: null, ajax Value
Today, I am writing and submitting a json data to the background, and then the background returns a json data type. However, it is found that the value obtained by request. getParamter ("") in the background is null.
Write a simple ajax request to troubleshoot the problem.
Front-end code:
$(document).ready(function(){ $("#ajax").click(function(){ var depart="depart"; $.ajax({ url :path+ "/AjaxReponse", data :"depart="+depart, type : "post", dataType : "json", success: function(data){ alert(data); } }); });});
Background code:
String depart=request.getParameter("depart");
Symptom: the value obtained in the background is null. However, when debugging the google chrome debugging tool, the request already contains the sent value.
I. Netizen Method
The content-type of a normal post request (excluding ajax requests) in the http header is application/x-www-form-urlencoded. In this case, you can use the request in the java background. getParameter (name. however, when a request is sent via native ajax, the request is sent in the java background. the passed parameter cannot be obtained in the form of getParameter (name.
However, we can see that content-type is already in the form of application/x-www-form-urlencoded. Therefore, the Netizen's method is not applicable.
Http://m.blog.csdn.net/blog/eyebrother/36007145
Ii. encoding format
Since the content sent by the request is correct in the debugger, the encoding format is incorrect. Add code in the background:
Request. setCharacterEncoding ("UTF-8 ");
This problem can be solved.
However, we found that the front-end is written as this type.
data :{ "depart" : depart },
Null is also obtained in the background. So we finally changed the js file encoding format to UTF-8.
3. Send json data to the background
$(document).ready(function(){ $("#ajax").click(function(){ var isReceipt = "1"; var adress ="2"; var reason = "3"; var projectInfo = { "adress" : isReceipt, "ownerDept" : { "deptCode" : adress }, "reason" : reason }; $.ajax({ url :path+ "/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 uses the JSON. stringify () method to convert the json type to the string type for sending.
Google's GSON package is used in the background, and then json data (String) is converted to object data type.
Iv. js tools
The compiler cannot perform the check because the js syntax is complex. Therefore, js Code styles are difficult to find errors. Two tools are recommended here.
1. JSTool
JavaScript code can be formatted
2. JSLINT
Check syntax errors
I will download the notepad ++ plug-in corresponding to these two tools. It is very convenient to use in notepad.
5. tucao
The new version of the csdn blog editor is really good, but it is not saved when writing a blog. When you open other csdn pages, the system prompts that the markdown editor instance is running and must be rewritten and loaded. Then the whole blog is rewritten. It cannot be tolerated!