1. 程式人生 > >js JSON.parse一直出現 Unexpected token o in JSON at position 1

js JSON.parse一直出現 Unexpected token o in JSON at position 1

VM152:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1
    at JSON.parse (<anonymous>)
    at Object.success (index.js:24)
    at j (jquery-1.11.0.min.js:2)
    at Object.fireWith [as resolveWith] (jquery-1.11.0.min.js:2)
    at x (jquery-1.11.0.min.js:4)
    at XMLHttpRequest.b (jquery-1.11.0.min.js:4)

對於這個錯誤我一直將解決問題的重心放在js上面,但是後臺才發現是我介面的問題:

看看我的介面:

	@RequestMapping(value = "/islogin", produces = "application/json; charset=utf-8")
	public @ResponseBody String isLogin(@RequestBody Map<String, String> reqMap,HttpSession session) {
		String token = reqMap.get("token");
		String loginname = "";
		String userguid = "";
		if ("WDGWILLSUCCESS".equals(token)) {
			loginname = session.getAttribute("loginname")+"";
			userguid = session.getAttribute("userguid")+"";
		}
		JSONObject json = new JSONObject();
		json.put("loginname", loginname);
		json.put("userguid", userguid);
		return json.toString();
	}

當然是能可以返回資料的,但是如果把引數上面的註釋:

 produces = "application/json; charset=utf-8"

去掉之後發現前臺的js就不報錯了

produces可能不算一個註解,因為什麼呢,它是註解@requestMapping註解裡面的屬性項,

它的作用是指定返回值型別,不但可以設定返回值型別還可以設定返回值的字元編碼;

還有一個屬性與其對應,就是consumes: 指定處理請求的提交內容型別(Content-Type),例如application/json, text/html;

希望對你有所幫助,下面是我的微信,歡迎大家一起學習交流: