从ajax调用webmethod并返回json [英] Call webmethod from ajax and return json

查看:126
本文介绍了从ajax调用webmethod并返回json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ajax调用从我的aspx页面调用webmethod。这个web方法正在上传文件,我需要返回的对象是Type Object。现在通过以下方式,该过程正在工作并成功上传文件,但我的gridView无法填充,因为返回的响应是XMLDocument类型:



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:

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();
        }
 });





在其他aspx页面中,我正在调用我的web服务并成功填充gridViews,因为返回的响应是Object类型。下面是一个例子:





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:

MyWebservice.MyWebMethod("test",LunchMyFunction);

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





现在我找到了一个使用Json的解决方案:





Now I have found a solution by using Json:

$.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]
         ....
  });





现在返回的r esponse是Object类型(这是完美的)但是我的webmethod无法捕获发送的文件,换句话说:我没有改变我的webmethod,但文件计数器:



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 :

HttpContext.Current.Request.Files.Count

为0,而在旧的ajax方法(第一个提到的方法)中,请求计数返回1并且文件上传成功。我的问题在哪里?如何正确发送我的文件并返回一个对象?



我尝试过的方法:



我也尝试将

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

<ScriptMethod(ResponseFormat:=ResponseFormat.Json)>

添加到我的webmethod但是当我想从ajax抛出内部服务器错误

to my webmethod but when I want to call from ajax an "internal server Error" is thrown

推荐答案

#FileUpload1)。get( 0 );
var files = fileUpload.files;
var fileData = new FormData();
fileData.append(files [ 0 ]。name,files [ 0 ]);
fileData.append(' username' ' 测试');
("#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'
类型: POST
contentType: false
processData: false
data:fileData,
成功: function (响应){
alert (响应); // 响应是[object XMLDocument]
var gridView =
.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,此过程在此停止
gridView。数据绑定();
}
});
find('<%= grdvUploadFiles.ClientID %>'); gridView.set_dataSource(response); // gridView could not be filled and the process stops here gridView.dataBind(); } });





在其他aspx页面中,我正在调用我的web服务并成功填充gridViews,因为返回的响应是Object类型。下面是一个例子:





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:

MyWebservice.MyWebMethod("test",LunchMyFunction);

function LunchMyFunction(response) {
   alert(response); // [object Object] 
   var gridView =


这篇关于从ajax调用webmethod并返回json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆