gpt4 book ai didi

java - 如何将模型解析为 ajax post 调用

转载 作者:行者123 更新时间:2023-12-02 11:05:09 24 4
gpt4 key购买 nike

我正在尝试使用 model.addAttribute("myModel",mymodelvariable); 解析我用 spring 加载的模型到 ajax 调用(该模型正在表单中使用)。

这是我使用@Html尝试的内容:

$('#sampleForm').submit(

function(event) {
var data = @Html.Raw(Json.Encode(Model));
$.ajax({
url: 'http://localhost:8080/hellohello',
type : "POST",
data : data,
beforeSend: function (xhr) {
xhr.setRequestHeader(header, token);
},
success : function(response) {
$('#reloading').html(response);
},
error : function(err) {
alert("not working")
console.log("not working. ERROR: "+JSON.stringify(err));
}
});
return false;
});

Controller :

 @CrossOrigin(origins = "http://localhost:8080")
@ResponseBody
@RequestMapping(value = "/hellohello",method = RequestMethod.POST)
public ModelAndView step1(@ModelAttribute Mymodelvariable mymodelvariable){

//do stuff and return model
}

最佳答案

试试这个可能对你有帮助

$('#sampleForm').submit(

function(event) {

var data={
"name":$('#name').val(),
"age":$('#age').val(),
"xyz":$('#xyz'+str).val(),
"xyz":$('#xyz'+str).val(),
"xyz":$('#xyz'+str).val(),
"xyz":$('#xyz'+str).val(),
"xyz":$('#xyz'+str).val(),

}
$.ajax({
url : "././hellohello",
type : "POST",
contentType : 'application/json; charset=utf-8',
data: JSON.stringify(data),
beforeSend: function (xhr) {
xhr.setRequestHeader(header, token);
},
success : function(response) {
$('#reloading').html(response);
},
error : function(err) {
alert("not working")
console.log("not working. ERROR: "+JSON.stringify(err));
}
});
return false;
});

Controller 类:

  @CrossOrigin(origins = "http://localhost:8080")
@RequestMapping(value = "/hellohello",method = RequestMethod.POST)
@ResponseBody
public ModelAndView step1(@RequestBody Modelclass mymodelvariable)throws
Exception{

//do stuff and return model
}

关于java - 如何将模型解析为 ajax post 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51029063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com