请参阅以下代码:
Public Class Default4
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim image1 As Image = Default4.GetSQLTable
OtherBits.Controls.Add(image1)
End Sub
<System.Web.Services.WebMethod()> _
Public Shared Function GetSQLTable() As Image
Dim Image1 As New Microsoft.Web.GeneratedImage
Image1.ImageHandlerUrl = "ImageHandler1.ashx?id=1"
Return Image1
End Function
End Class
代码运行后,网页上有一个图像(OtherBits是占位符)。
我想使用AJAX在客户端检索图像,即AJAX将调用一个函数(获取SQL表),该函数调用图像处理程序。这是客户端代码:
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/LGMaster.master" CodeBehind="Default4.aspx.vb" Inherits="com.app.PresentationLayer.Web.Default4" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.1.min.js"></script>
<script type = "text/javascript">
function GetSQLTable() {
$.ajax({
type: "POST",
url: "Default4.aspx/GetSQLTable",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess(),
failure: function (response) {
alert('there was an error loading the webpage')
}
});
function OnSuccess() {
return function (response) {
document.getElementById('test_div').innerHTML = response.d;
}
}
}
window.onload = GetSQLTable
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div id="test_div">
</div>
<asp:PlaceHolder runat="server" ID="OtherBits" />
</asp:Content>
我在ImageHandler中放置了一个断点,并且可以确认如果客户端是AJAX则不会调用它。为什么不使用AJAX客户端调用图像处理程序?将记录对图像处理程序的所有调用。我可以确认它肯定没有被调用。
答案 0 :(得分:0)
我不确定,但我认为这可能与共享功能的限制有关(正如我所说,我不确定) 但是你尝试在ASHX中创建图像,完全删除GetSQLTable并将ajax URL设置为:
url: "ImageHandler1.ashx?id=1",
而不是:
url: "Default4.aspx/GetSQLTable",
为什么需要GetSQLTable?