【发布时间】:2021-02-13 00:06:44
【问题描述】:
空数据是通过jquery从html页面传输的,请帮助我 这是我的 api 代码 asp.net 核心,它工作正常
[HttpPost]
public async Task<ActionResult<LeaveSubmit>> Submitdata()
{
try
{
LeaveSubmit lobj = new LeaveSubmit();
// string myobj = Convert.ToString(lobj);
JObject jsonObject = JObject.FromObject(lobj);
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);
}
}
当我点击提交按钮发送到 api 时的 jquery 代码为空,因此空数据保存在 json 页面中 //数据保存。//`$("#btnSubmit").click(function () { 数据传递(); });
function datapass() {
var values = {};
var values = $("#notesval").val();
var myvalue = JSON.stringify(values);
$.ajax({
url: window.location.origin + "/api/ServiceApi/Submitdata",
type: 'POST',
dataType: 'json',
data: myvalue,
contentType: 'application/json; charset=utf-8',
success: function () {
//console.log("Savesuccessful");
//console.log(data);
alert("Record Save Succesfully");
//$(".loader").hide();
},
error: function (error) {
alert("Error is occured");
//console.log("My errror values:", error);
}
});
}
// Date save close.//`
【问题讨论】:
-
可以显示
$("#notesval").val()的内容吗? -
我要保存内容 $("#notesval").val()
-
它是一个对象?因为你在之后字符串化?你确定 myvalue 是 json 格式吗?
-
你需要做两件事:1在javascript中你应该像这样创建你的对象 values = { MyProperty: $("#notesval").val() };然后你把它串起来。 2 在您的控制器/动作中,您需要一个参数 Submitdata([FromBody] MyClass myParam) 并且 MyClass 应该有一个名为 MyProperty 的属性。基本上,您的参数需要与您从 ajax 调用中作为 json 发送的对象相匹配
标签: jquery asp.net-core