I'm trying to pass three parameters to a WebMethod using AJAX in an old ASP.NET web application (NET Framework 4.6.2 project).
Note: I cannot use a form, so the three controls (id, date and tempdocument) are just wrapped in a <div> element (so not in <form>).
Here is my javascript code:
function passValues() {
// this one is from an <input> (text) control
var date= $('#txbDate').val();
// this one is from a <input> (date) control
var id= $('#txbId').find('select').val();
// this one is from an <input> (type=file) control
var filedata = $("#TempDocument").get(0).files;
// url to the aspx.file where the WebMethod is
var url = 'Test.aspx/PassValues';
$.ajax({
type: 'post',
url: url,
processData: false,
contentType: false,
data: { id: id, date: date, filedata: filedata}
}).done(function (result) {
// do some stuff here
});
}
and I'm trying to catch these in a webmethod:
[WebMethod]
public static string PassValues(int id, string date, string filedata)
{
// do some stuff here
// return some stuff in a Json result
}
When running passValues() I get no javascript errors and All three variables (id, date and filedata) get populated, but the WebMethod does not get hit.
I do however get a 302 network error (browser developer tools) with no details about the error.
What is the reason for the error? Is it even possible to pass files with AJAX in an ASP.NET app?
Your data has to take the varibles and make a string.
It can be some messy js code to do that, but you can use stringify, and it will do the work for you. I mean, when you use id : id, how does it know that "id" supposed to be "id", or the value of id? Which will you get?
So, try this:
Edit: Try building a test web page
Ok, so when you build a web method, you can create a web service page (.asmx), or you can drop (place) web methods right into a existing page.
so, create a new web page - and test/try adding a web method.
Assuming you do have jQuery working (installed) then try create a new web page - and don't include a master page (if you using them).
So, you have a page that looks like this:
And the code behind looks like this:
So, try a test page.
Edit#: Getting value of a FileUpLoad control
Your code of this looks to be wrong:
I Can't see how above will work????
This works: