【问题标题】:jQuery ajax returning entire aspx contentjQuery ajax 返回整个 aspx 内容
【发布时间】:2018-09-21 11:10:50
【问题描述】:

我正在尝试使用 jQuery 到 C# 方法进行 ajax 调用。

$(".imgDbAttachment").on("click", function (e) {
            debugger;
            var fileName = $(this).attr('data-attchment-id');
            fileExt = $(this).attr('data-attchment-type');
            loadAjaxImage(fileName, fileExt);
        });

 function loadAjaxImage(id,type) {
            $.ajax({
                type: "POST",
                url: "../CommonDesign/Test.aspx/GetImage",
                data:{
                    'attachmentId': id,
                },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    console.log(data);
                }
            }).done(function (data) {
                if (console && console.log) {
                    console.log(data);
                }
            });
        }


public partial class Test: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        var str = this.Request.Url;
    }


   [WebMethod]
    public string GetImage(string attachmentId)
    {

             return "hello";
    }

但是当我进行 ajax 调用时,控件正在点击 PageLoad() 而不是 GetImage() & 这反过来又返回整个 aspx 页面内容

检查这些链接,

  1. $.ajax Returning HTML of the page instead of results
  2. 还有一些其他的

但还是同样的问题。

任何建议/高度赞赏。

【问题讨论】:

    标签: c# jquery asp.net ajax webmethod


    【解决方案1】:

    使GetImage() 静态

    [WebMethod]
    public static string GetImage(string attachmentId)
    {
       return "hello";
    }
    

    更多阅读here

    【讨论】:

    • 已检查,但这会引发 500 内部服务器错误
    • 在删除 contentType: "application/json; charset=utf-8" 时,它运行但仍然是相同的整页内容
    • 您可以留下contenType 并执行console.log(data.d);
    【解决方案2】:

    当您单击按钮时,您似乎也在发布帖子。最好加个

    return false; 
    

    到函数的最后一行

    $(".imgDbAttachment").on("click", function (e) {
                debugger;
                var fileName = $(this).attr('data-attchment-id');
                fileExt = $(this).attr('data-attchment-type');
                loadAjaxImage(fileName, fileExt);
    return false;
            });
    

    【讨论】:

      猜你喜欢
      • 2013-03-20
      • 2011-02-04
      • 1970-01-01
      • 2016-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      相关资源
      最近更新 更多