Click here to Skip to main content
15,885,092 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Below is the my ajax request
JavaScript
function sendData() {
    var formdata = new FormData();
    var fileUpload = $("#txtUploadFile").get(0);
    var files = fileUpload.files;   
    for (var i = 0; i < files.length; i++) {
        formdata.append(files[i].name, files[i]);
        }
    formdata.append("PaymentDate", new Date());
    $.ajax({
        url: 'CCA_Form.aspx/SendData',
        type: 'POST',
        data: formdata,
        contentType: false,
        processData: false,
        success: function () {
            alert("Data Added Successfully");
        },
        error: function () {
            alert("Error while inserting data");
        }
    });
}
and my server method is like this
C#
[WebMethod]
    public static string SendData()
    {//break point here
        // code
        return "return data";
    }
the ajax method always showing success message and webmethod not hitting in server side. Could you help me what i missed in my code? Thanks in advance.

What I have tried:

How to send formdata to aspx.cs page using jquery ajax?
Posted
Updated 2-May-17 19:00pm

Obvious webMethod will not going to execute

You have passed data in AJAX POST method

data: formdata,


But there is no parameters in your webmethod.

[WebMethod]
    public static string SendData()
    {//break point here
        // code
        return "return data";
    }


I hope it is enough for you, Let me know if you have any query or concern for same.
 
Share this answer
 
Comments
sandeep nagabhairava 3-May-17 1:28am    
Dear Nirav Prabtani, Could you tell me what is the parameter type in WebMethod? bcoz i'm sending FormData it includes various inputs.
Nirav Prabtani 3-May-17 2:28am    
You have to declare parameter according to formdata
support your formdata = {"ID":"1","Name":"nirav p"}

than your webmethod will be
[WebMethod]
public static string SendData(string ID,string Name)
{//break point here
// code
return "return data";
}
[WebMethod(EnableSession = true)]
public static string SendData()
{//break point here
// code
return "return data";
}

$.ajax({
contentType: false,
data: formData,
url: "/CCA_Form.aspx/SendData",
type: "POST",
processData: false,
success: function (data) {
alert("Data Added Successfully");
},
error: function () {
alert("Error while inserting data");
}
})
Try this.
 
Share this answer
 
v2
Comments
sandeep nagabhairava 3-May-17 0:13am    
@Ramesh Kumar Barik what is the parameter for FormData? it includes all input content
Ramesh Kumar Barik 3-May-17 0:37am    
Dear Sandeep,
I have updated my answer please check and let me know whether it work.
Only add [WebMethod(EnableSession = true)]

Thanks.
sandeep nagabhairava 3-May-17 1:30am    
Dear Ramesh,
It's not working, I'm sending as FormData(contains all input), but i'm not sure how to get it in server side.
Ramesh Kumar Barik 3-May-17 1:51am    
Dear Sandeep,
The same code I have written in my application it working fine for me.
Please check in debug mode what is the issue.

Thanks
Ramesh Kumar Barik 3-May-17 1:58am    
Dear Sandeep,

You can refer this link.

https://forums.asp.net/t/2024764.aspx?Passing+form+data+to+a+controller+method+using+Ajax

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900