首页 > 解决方案 > 如何在我的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();
        }
    }

标签: jqueryasp.netajaxapiasp.net-core

解决方案


 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();
            }

推荐阅读