0

My code int Client:

var formData = new FormData();
        formData.append("ID", "1");
        formData.append("Name", "Gà Haha");
        console.log(formData.get("ID"));
        $.ajax({
            type: "POST",
            url: "http://localhost:13497/myapi/student",
            contentType: "json",//Request header
            data:formData,
            dataType: "json",//Responce header
            processData: false,
            success: function (data) {
                $.each(data, function (key, value)
                {
                    var jsonData = JSON.stringify(value);
                    var objData = $.parseJSON(jsonData);
                    var id = objData.ID;
                    var fname = objData.Name;
                    var lname = objData.Price;
                    $('<tr><td>' + id + '</td><td>' + fname + '</td><td>' + lname + '</td></tr>').appendTo('#students');
                });
            },
            error: function (errormsg) {
                alert(errormsg.responseText);
            }
        });

My code in server:

 [HttpPost]
    public IEnumerable<Products> GetStudents()
    {
        string temp=HttpContext.Current.Request.Params["ID"];
        return temp;
    }

But temp return null. I'm using How can retrieve string formData js in c# but return empty and here How to read FormData into Web api but it not working.

Finally: I want to get data from FormData Client send

  • try to hide `contentType: "json",` and `dataType: "json",` from your ajax call and let me know – er-sho Dec 11 '18 at 10:25
  • Tried! But It not working – Please Help Me Dec 11 '18 at 10:29
  • your ajax url contains `student` but there is no any api exist with this name. if you want to hit `GetStudents` then replace `student` with `GetStudents` in url – er-sho Dec 11 '18 at 10:50
  • I'm found my error. Maybe cause is URL Friendly, So, I'm comment URL Friendly. But Im unknown call back my function. Please help me – Please Help Me Dec 11 '18 at 10:58
  • First you should fix your request and ensure it's send as form data with correct headers. You can read about that e.g. here: https://stackoverflow.com/questions/6974684/how-to-send-formdata-objects-with-ajax-requests-in-jquery and check it using your browsers developer tools. – Christoph Lütjen Dec 11 '18 at 11:00
  • Root folder. Please help me call back my function – Please Help Me Dec 11 '18 at 11:01
  • in network tab of browser watch your ajax call properly sent posted data to api. And also check once your url is pointed to desired api i.e. `GetStudents` – er-sho Dec 11 '18 at 13:52

1 Answers1

0

Please try in server side

Use form

HttpContext.Current.Request.Form["ID"] instead of Request.Params["ID"]

Satheesh
  • 171
  • 6