How to pass multiple data in my ajax query and save the data by api

I want to save the more than values from jquery ajax to web api please help me for this.
Here is my asp.net core web api code

[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 code. I have successfully save one values called notes and i want to save one more values but I cannot pass the values its send the null so api saved the values with null values

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x