Click here to Skip to main content
15,904,494 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am calling a webmethod from my aspx page using ajax call. This webmethod is uploading files and I need the returned object to be of Type Object. Now in the below way the process is working and uploading files successfully, but my gridView could not be filled because the returned response is of type XMLDocument:

JavaScript
var fileUpload = $("#FileUpload1").get(0);
var files = fileUpload.files;
var fileData = new FormData();
fileData.append(files[0].name, files[0]);
fileData.append('username', 'Test');
$.ajax({
        url: 'MyWebservice.asmx/UploadFiles',
        type: "POST",
        contentType: false,
        processData: false,
        data: fileData,
        success: function (response) {
           alert (response);  // response is [object XMLDocument]
           var gridView = $find('<%= grdvUploadFiles.ClientID %>');
           gridView.set_dataSource(response);
           // gridView could not be filled and the process stops here
           gridView.dataBind();
        }
 });


In other aspx pages I am calling my webservice and filling gridViews successfully because the returned response is of type Object. Below is an example:

JavaScript
MyWebservice.MyWebMethod("test",LunchMyFunction);

function LunchMyFunction(response) {
   alert(response); // [object Object] 
   var gridView = $find('<%= MyGridView.ClientID %>');
   gridView.set_dataSource(response);
   gridView.dataBind();
}


Now I have found a solution by using Json:

JavaScript
$.ajax({
         type: "POST",
         url: 'AutoComplete.asmx/UploadFiles',
         data: JSON.stringify(fileData),
         contentType: "application/json; charset=utf-8",
         processData: false,
         dataType: "json",
         success: function (response) {
         alert(response); // [object Object]
         ....
  });


Now the returned response is of type Object (Which is perfect) BUT my webmethod could not catch the sent file, in other words: I haven't change my webmethod , but files counter :
VB
HttpContext.Current.Request.Files.Count
is 0, while in the old ajax method (the first one mentionend) , the request count is returning 1 and file is uploaded successfully. Where is my problem? How can I send my file properly and return an object?

What I have tried:

I have tried also to add
VB
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)>
to my webmethod but when I want to call from ajax an "internal server Error" is thrown
Posted
Updated 3-Jan-17 21:57pm

1 solution

Check the content type for both the request, you will get your answer.
contentType: "application/json; charset=utf-8",
Server will look for content type and in refactored method it will mark as JSON object.

Thanks,
Ashwin Shetty
 
Share this answer
 
Comments
H.AL 4-Jan-17 4:09am    
I did not understand What you mean, can you give me an example please?
Ashwin. Shetty 4-Jan-17 4:22am    
When you are making a call from ajax (within browser), you are setting content type to application/json but at server, you are expecting file. For file upload content type should be set to multipart/form-data.
H.AL 4-Jan-17 4:35am    
And how can I get returned data as Json? If I add contentType: "multipart/form-data", and dataType: "json" and data: fileData an error "OK" is returned . What's happening here?
Ashwin. Shetty 4-Jan-17 4:47am    
In case of error, you can return http error code 500. Try setting content type while responding back from server

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