Javascript 如何使用jqueryajax调用将texbox和attachement发送到webmethod?
我正在尝试将文本框和输入文件数据发送到我的webmethod。我已经在谷歌上搜索了很长一段时间,但仍然不确定如何实现这一点: Jquery/AJAX调用: WebMethod: 似乎我无法调用webmethod 正如卡西夫所建议的那样: *试试这个。编辑一个。在这里,我使用Btoa将文件转换为base64Javascript 如何使用jqueryajax调用将texbox和attachement发送到webmethod?,javascript,c#,jquery,asp.net,ajax,Javascript,C#,Jquery,Asp.net,Ajax,我正在尝试将文本框和输入文件数据发送到我的webmethod。我已经在谷歌上搜索了很长一段时间,但仍然不确定如何实现这一点: Jquery/AJAX调用: WebMethod: 似乎我无法调用webmethod 正如卡西夫所建议的那样: *试试这个。编辑一个。在这里,我使用Btoa将文件转换为base64 dataToSend: "{'file' : " + btoa(document.getElementById("myFile").value)+ ",'biddername':" + do
dataToSend: "{'file' : " + btoa(document.getElementById("myFile").value)+ ",'biddername':" + document.getElementById("biddername").value+ "}",
$.ajax({
type: "POST",
url: "YourWebMethodClassName.aspx/SubmitBid",
data: dataToSend,
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);
}
});
}
C-编辑代码
[WebMethod]
public static string RegisterSupplier(string file, string biddername)
{
// Write here logic to convert Base64 to file.
return "a";
}
您不能发布文件的内容。你必须在文件中附上一份表格。您应该搜索如何将带有文件输入的表单发布到web方法。首先,您尝试在ajax中调用的url是SupplierMaster.aspx/RegisterSupplier,但是您没有名为RegisterSupplier的方法,而是向我们显示名为SubmitBid的方法???@gurupasadrao抱歉,我编写了webmethod而不是复制粘贴。我已经更新了问题,但问题仍然存在。您是否尝试设置断点??是的,我一直在尝试,但不是,它没有尝试将数据类型设置为类似json的数据类型:json、[System.Web.Services.webmethod][System.Web.Script.Services.ScriptMethod]将此添加到c方法上我根据您的建议再次更新了我的问题。它仍然没有击中WebMethod再次编辑此。我没有建议。processData:false、contentType:false、dataType:false,这在您这边有效吗?因为它不是我的。您在RegisterSupplier中将HttpPostedFile更改为字符串了吗?
$.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";
}
dataToSend: "{'file' : " + btoa(document.getElementById("myFile").value)+ ",'biddername':" + document.getElementById("biddername").value+ "}",
$.ajax({
type: "POST",
url: "YourWebMethodClassName.aspx/SubmitBid",
data: dataToSend,
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);
}
});
}
[WebMethod]
public static string RegisterSupplier(string file, string biddername)
{
// Write here logic to convert Base64 to file.
return "a";
}