jquery - 如何在我的ajax查询中传递多个数据并通过api保存数据
问题描述
我想将 jquery ajax 中的多个值保存到 web api 请帮助我。 这是我的 asp.net 核心 web api 代码
[HttpPost]
public async Task<ActionResult<LeaveSubmit>> Submitdata([FromBody] LeaveSubmit myobj)
{
try
{
// string myobj = Convert.ToString(lobj);
JObject jsonObject = JObject.FromObject(myobj);
if (jsonObject == null)
{
return BadRequest();
}
else
{
var json = System.IO.File.ReadAllText(jsonFile);
var jsonObj = JObject.Parse(json);
var experienceArrary = jsonObj.GetValue("Notes") as JArray;
var newnotes = jsonObject;
experienceArrary.Add(newnotes);
jsonObj["Notes"] = experienceArrary;
string newJsonResult = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented);
System.IO.File.WriteAllText(jsonFile, newJsonResult);
// var response = await newJsonResult;
return Ok("Success");
}
}
catch (Exception ex)
{
this.telemetryClient.TrackException(ex);
return this.StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
}
jquery ajax 代码。我已成功保存了一个名为 notes 的值,我想再保存一个值,但我无法传递其发送 null 的值,因此 api 保存了具有 null 值的值
$("#btnSubmit").click(function () {
datapass();
});
function datapass() {
var values = { notes: $("#notesval").val() };
var myvalue = JSON.stringify(values);
//myvalue = "{"notes":""}", values = { notes: "" }
if (values == '{ notes: "" }' || myvalue == '{"notes":""}' || myvalue == null || values == null) {
return false;
}
else {
$.ajax({
url: window.location.origin + "/api/ServiceApi/Submitdata",
type: 'POST',
dataType: 'json',
data: myvalue,
contentType: 'application/json; charset=utf-8',
success: function (response) {
response = eval(response);
OnSuccess("Success");
//console.log("Savesuccessful");
//console.log(data);
},
error: function (error) {
alert("Save sucessfully.");
//console.log("My errror values:", error);
}
});
$("#confirm-modal").show();
}
}
解决方案
var leavetypeval = $("#ddlLeaveType").val();
var values = $("#notesval").val();
//var myvalue = JSON.stringify(leavetypeval);
//var myvalue2 = JSON.stringify(values);
//myvalue = "{"notes":""}", values = { notes: "" }
if (leavetypeval == "" || values == "" || leavetypeval == null || values == null) {
return false;
}
else {
$.ajax({
url: window.location.origin + "/api/ServiceApi/Submitdata",
type: 'POST',
dataType: 'json',
data: JSON.stringify({ leavetype: leavetypeval, notes: values }),
contentType: 'application/json; charset=utf-8',
success: function (response) {
response = eval(response);
OnSuccess("Success");
//console.log("Savesuccessful");
//console.log(data);
// alert("Save sucessfully.");
},
error: function (error) {
// alert("Save sucessfully done.");
//console.log("My errror values:", error);
}
});
$("#msgtxt").show();
}
推荐阅读
- vba - 找不到`elementId`时提示
- c - 在 C 编程中创建函数,使用 while 循环时出错
- assembly - MIPS - 释放动态分配的空间(堆)
- python - 如何解决在sql中获取行时内存消耗增加的问题?
- reactjs - 我想了解我通过 create-react-app 创建的 React(16.13.1)
- google-apps-script - 如何准确地使用 appscript 的 UrlFetchApp 抓取谷歌搜索结果?
- python - Python - MYSQL 动态查询:将值插入表语法
- excel - Excel 嵌套 IF 问题,用于检查前面列中的重复名称
- php - 添加到 smarty 模板页面 (.tpl) 时,Google 图表不显示
- python - 如何在pygame中修复球与矩形对象的碰撞