我需要你的帮助。我必须将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