<script>
$.ajax({
type: "POST",
url: "UploadPicture.aspx/ModulePhotoUpload",
data: { argjson: argjson, menuid: menuid },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(success) {
alert(success);
// Do something interesting here.
}
});
</script>
body中增加ScriptManager
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<script>
PageMethods.ModulePhotoUpload(argjson, menuid, InsModulePhotoCallBack);
function InsModulePhotoCallBack(success) {
alert(success);
}
</script>
后台定义方法可以用:aspx、ashx、asmx、谨记,千万千万别在ascx用户控件后台写。用户控件是无法通过Ajax访问的,会出现“无法访问ascx”错误,PageMethods访问则会报“PageMethods未定义”错误。
/// <summary>
/// 图片上传结果处理
/// </summary>
/// <param name="arg"></param>
/// <returns>1 上传完成,2上传失败</returns>
[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod()]
public static string ModulePhotoUpload(string argjson, int menuid)
{
//此处实现业务代码
return "上传完成";
}