【发布时间】:2015-05-14 06:39:59
【问题描述】:
我正在尝试将文本框和输入文件数据发送到我的网络方法。我已经在谷歌上搜索了很长一段时间,但仍然不确定如何实现这一目标:
Jquery/AJAX 调用:
var dataToSend = new FormData();
dataToSend.append('file', document.getElementById("myFile").value);
dataToSend.append('text', document.getElementById("biddername").value);
$.ajax({
type: "POST",
url: "SupplierMaster.aspx/RegisterSupplier",
data: dataToSend,
processData: false,
contentType: false,
dataType: false,
async: true,
success: function (data, status) {
console.log("CallWM");
alert(data.d);
},
failure: function (data) {
alert(data.d);
},
error: function (data) {
alert(data.d);
}
});
}
网络方法:
[WebMethod]
public static string RegisterSupplier(HttpPostedFile file, string biddername)
{
return "a";
}
我好像无法调用 webmethod。
EDIT1(根据 Kashif 的建议):
$.ajax({
type: "POST",
url: "SupplierMaster.aspx/RegisterSupplier",
data: "{'file' : " + document.getElementById("myFile").value + ",'biddername':" + document.getElementById("txtsuppliername").value + "}",
async: true,
contentType: "application/json; charset=utf-8",
success: function (data, status) {
console.log("CallWM");
alert(data.d);
},
failure: function (data) {
alert(data.d);
},
error: function (data) {
alert(data.d);
}
});
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static string RegisterSupplier(HttpPostedFile file, string biddername)
{
return "a";
}
*
【问题讨论】:
-
首先,您尝试在 ajax 中调用的
url是SupplierMaster.aspx/RegisterSupplier,但您没有名称为RegisterSupplier的方法,而是向我们展示名称为SubmitBid的方法? ? -
@GuruprasadRao 对不起,我写了 web 方法而不是复制粘贴。我已经更新了问题,但问题仍然存在
-
你试过放断点吗?它是在使用那个网络方法吗??
-
是的,我一直在尝试,但不是没有达到那个 webmethod
-
尝试将
datatype设置为json,例如dataType: "json",
标签: javascript c# jquery asp.net ajax