我需要你的幫助。我必須將json對象發送給spring mvc控制器並將其取回並在視圖中的字段中進行替換。我的項目是Spring MVC。這對我來說是新的。我發佈了Controller和jQuery代碼。將json對象傳遞給控制器
setOperator = function(newOperator) {
// alert("operator " + newOperator);
if (newOperator == '=') {
// alert("newOperator is = ");
//AJAX, JSON
json_result = {"jsn_result" : lastNumber};
$.ajax({
type: "POST",
contentType : 'application/json; charset=utf-8',
dataType : 'json',
url: "http://localhost:8080/calc",
data: JSON.stringify(json_result), // Note it is important
success :function(result) {
console.log(json_result.jsn_result);
}
});
equalsPressed = true;
calculate();
return;
}
if (!equalsPressed) {
// alert("followed by an operator (+, -, *, /)");
calculate();
}
equalsPressed = false;
operator = newOperator;
operatorSet = true;
lastNumber = parseFloat(currNumberCtl.val());
},
@Controller
@RequestMapping("/")
public class CalculatorController {
\t @RequestMapping(method = RequestMethod.GET)
\t public String printWelcome(ModelMap model) {
\t \t return "calculator";
\t }
}
歡迎使用stackoverflow!請閱讀[如何提問](http://stackoverflow.com/help/how-to-ask),並分享您迄今嘗試使用的代碼。 – user1859022
請將代碼作爲文本添加到「代碼」部分。 – zx485
我必須在控制器中包含哪些更改? – Maria