jquery was not called solution

Keywords: JSON Java

1. Writing ajax requests today requires cross-domain requests, starting with the following code:

Front-end code:

 var url = "http://localhost:8080/wechat/wxtestsurvey/getTestList.dol"; 
$.ajax({
			url:url, 
			type:'POST',
			cache:false,
			data:{"openId":openid}, 
			dataType: 'jsonp',
			jsonp:'jsoncallback',
			crossDomain: true,
			success:function(data) {
alert(data);
 },
error: function(XMLHttpRequest, textStatus, errorThrown){
				//alert("system maintenance, please try again later! "";
				 alert(XMLHttpRequest.status);   // 200    
				 alert(textStatus);   // parsererror    
				 alert(errorThrown);  // SyntaxError: Unexpected end of input 
			}
	  });
java code:

        @RequestMapping(value = "/getTestList", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getTestList(String openId,HttpServletRequest request){
		
		String jsoncallback = request.getParameter("jsoncallback");// Client request parameters
		JSONObject json = new JSONObject();
		HashMap<String,Object> map = new HashMap<String, Object>();
		map.put("name", "nick");
		map.put("age", "23");
		map.put("sex", "M");
		map.put("code", "0");
		return jsoncallback + "("+JSONObject.fromObject(json)+")";
	}
It turned out to be an ajax error, which is really beyond comprehension. The results are as follows:

Error: jQuery19105265967122703629_1498552272260 was not called

But the response does return a json string, as follows:

"jQuery19108484969351256605_1498551586758({\"code\":\"0\",\"name\":\"nick\",\"age\":\"23\",\"sex\":\"M\"})"

Uh huh! You're not mistaken. It's a real string.

Go and find Du Niang, find and find! It's been a whole day, a whole day!

But it didn't work out.

There are many statements on the Internet, but what they say meets the requirements, but it is useless.

The next day I looked again, and I saw that $. getJSON() could also make cross-domain requests, and then I tried.

The result is the same, that is, no success.

My whole body is running to pieces, and this problem has been going on for so long.

Slowly I calmed down, and I remembered that I had written cross-domain background code before, but unlike this, I started to turn over the code I wrote before, eh! I found it, and I tried it. I don't have much hope for it. Really! But I succeeded. I was strangled, and suddenly a hundred thousand grass-mud horses flew by in my heart.

The changed code is as follows:

   @RequestMapping(value = "/getTestList", produces = "application/json;charset=utf-8")
	@ResponseBody
	public void getTestList(String openId,HttpServletRequest request,HttpServletResponse response){
		
		String jsoncallback = request.getParameter("jsoncallback");// Client request parameters
                String result = "";
               JSONObject json = new JSONObject();
		HashMap<String,Object> map = new HashMap<String, Object>();
		map.put("name", "nick");
		map.put("age", "23");
		map.put("sex", "M");
		map.put("code", "0");
	       result = jsoncallback + "("+JSONObject.fromObject(json)+")";
        try {
            response.getWriter().print(result);
        } catch (IOException e) {
            logger.info("Abnormal:",e);
        }
	}

The response data are as follows:

jQuery19104063224993617034_1498551266153({"code":"0","name":"nick","age":"23","sex":"M"})

Finally, the response was successful!

I wrote this to give myself a warning and hope to help meet friends who are in the same situation as me.


Tell me why.

Because the first return is a pure string, not a json string.

The second return is the json string.


As for why, it remains to be studied.


Posted by Stripy42 on Tue, 18 Jun 2019 13:49:07 -0700