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

7
  • try to hide contentType: "json", and dataType: "json", from your ajax call and let me know Commented Dec 11, 2018 at 10:25
  • Tried! But It not working Commented Dec 11, 2018 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 Commented Dec 11, 2018 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 Commented Dec 11, 2018 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: stackoverflow.com/questions/6974684/… and check it using your browsers developer tools. Commented Dec 11, 2018 at 11:00

1 Answer 1

0

Please try in server side

Use form

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.