jquery - $.get、$.post、$.ajax、$(elm).load到.ashx页面问题

标签 jquery ashx

HTML 页面

    // in script tag
    $(document).ready(function () {
        var url = "list.ashx";

        $.get(url + "?get", function (r1) { alert("get: " + r1); });
        $.post(url + "?post", function (r2) { alert("post: " + r2); });
        $.ajax(url + "?ajax", function (r3) { alert("ajax: " + r3); });
        $("div:last").load(url + "?load", function (r4) { alert("load: " + r4); });
    });

    // in body tag
    <div></div>

在“list.ashx”中

public void ProcessRequest (HttpContext context) { context.Response.Write("ok"); }

结果

  • $.get 和 $.post 到达 list.ashx 但 不返回
  • $.ajax 未到达 list.ashx
  • $.load 完全成功

问题是

  • 为什么只有“$.load”有效?
  • 如何使 $.get 或 $.post 工作?

更新

        $("input").click(function () {
            $.ajax({ url: url
                , context: this
                , data: "ajax=test"
                , cache: false
                , async: false
                , global: false
                , type:"POST"
                , processData: false
                , dataType: "html"
                , success: function (data) { alert(data); }
                , error: function (data) { alert(data.responseText); }
                });
        });

总是遇到错误:function(){},但“data.responseText”是正确的结果!!

最佳答案

嗯,你的 $.ajax() 不起作用的原因是它是 syntactically invalid 。它应该看起来更像这样:

$.ajax({
    type: "POST", // or "GET"
    url: "list.ashx",
    data: "postvar=whatever",
    success: function(r3){
       alert("ajax: " + r3);
    }
});

另外,当使用$.get$.post时,您应该将数据放在第二个参数中:

$.get(url, 'getvar=whatever', function (r1) { alert("get: " + r1); });
$.post(url, 'postvar=whatever', function (r2) { alert("post: " + r2); });

// or use a map

$.get(url, { getvar : 'whatever' }, function (r1) { alert("get: " + r1); });
$.post(url, { postvar : 'whatever' }, function (r2) { alert("post: " + r2); });

关于jquery - $.get、$.post、$.ajax、$(elm).load到.ashx页面问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4450139/

相关文章:

jquery - .ashx 处理程序不在服务器上工作,仅在本地工作

javascript - Stripe - Elements、jQuery 修改问题?

jquery - Rowcommand 没有为 gridview 中的 linkbutton 触发?

web-config - 如何使用 .ashx 文件在 IIS 7.0 集成模式下注册 HttpHandler

javascript - 如何在javascript或jquery中获取工作文件夹的物理路径?

c# - 通用处理程序文件下载未开始

jquery - 如何使用 jQuery 创建切换按钮

javascript - 生产中的手机/平板电脑浏览器上出现奇怪的 JS 错误

javascript - 如何在 iframe 中发生点击时触发事件?

javascript - 如何从 JavaScript 调用 ASHX?