【问题标题】:How to pass parameter through url using jquery ajax without refreshing page?如何使用jquery ajax通过url传递参数而不刷新页面?
【发布时间】:2014-04-19 08:08:17
【问题描述】:

我正在尝试使用 Asp.Net 中的 Get 方法传递参数。但是地址栏里的url没有变化。

请任何人帮助我通过 url 使用 ajax 调用传递参数。

我这边的一些尝试如下考虑url如下

        var obj = { templateName: templateName, pageIndex: pageIndex };
        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            url: "result.aspx/DisplayResult",
            // data: "{'templateName':'" + document.getElementById('txtSearch').value + " &pageIndex : '" + pageIndex + "'' }",
            data: JSON.stringify(obj),
            dataType: "json",
            success: OnSuccess,
            error: function (result) {
                alert(result.value);
            }
        });

【问题讨论】:

  • 您无需在obj 上申请JSON.stringify()。应该只是:data: obj,...
  • 按照问题中提到的方式传递数据有什么问题?如果你想改变 url,你可以通过 javascript 代码来完成,并且仍然像你一样传递数据。如果你只想通过 url 传递数据,你应该使用 POST 方法而不是 GET。

标签: javascript jquery asp.net ajax


【解决方案1】:

您的 AJAX 请求看起来不错,但没有理由更改 URL。如果您的请求成功,它将由OnSuccess 函数处理。但是,我看不到您在哪里定义了onSuccess 引用的函数。试试这个:

   var obj = { templateName: templateName, pageIndex: pageIndex };
        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            url: "result.aspx/DisplayResult",
            // data: "{'templateName':'" + document.getElementById('txtSearch').value + " &pageIndex : '" + pageIndex + "'' }",
            data: JSON.stringify(obj),
            dataType: "json",
            success: function (response) {
              // You should see the response object in your dev console
              console.log(response);
              // If you want to manipulate the URL for some reason after a successful callback,
              // do that here, or better, call a function referenced elsewhere that does it.
            },
            error: function (result) {
                alert(result.value);
            }
        });

【讨论】:

  • 但是在url中我没有找到通过参数传递的变量。
  • 我不太确定你在问什么。你的意思是说你的GET请求从服务器返回后,你浏览器中的URL没有变化吗?
  • 是的,当页面更改或单击返回按钮而不刷新页面时,我必须更改浏览器中的 url。
猜你喜欢
  • 2011-07-11
  • 2015-12-04
  • 1970-01-01
  • 2011-01-02
  • 2021-05-20
  • 2021-07-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多