window.location 更改失败 AJAX 调用
我有一个点击跟踪 AJAX 函数(在 .aspx
页面上调用 WebMethod
),当用户点击链接时我需要调用它。
不幸的是,我使用 window.location = "newUrl"
来更改页面,这似乎使 AJAX 调用失败。有办法解决这个问题吗?
我不需要从 AJAX 调用中获取任何信息,我只需要确保调用 WebMethod
即可。
我知道我可以重定向 success()
或 failure()
调用,但随后我必须等待 clickTracking()< /code> 方法运行,需要 ~
1s
。这在项目规范中是不可接受的,因此不是一个可行的解决方案。
I've got a click tracking AJAX function (calls a WebMethod
on an .aspx
page), and I need to call it when the user clicks through to a link.
Unfortunately, I'm using window.location = "newUrl"
to change the page, which seems to make the AJAX call fail. Is there a way around this?
I do not need to get any information back from the AJAX call, I just need to make sure the WebMethod
is called.
I'm aware that I could just redirect on the success()
or failure()
calls, but then I would have to wait for the clickTracking()
method to run, which takes ~ 1s
. That is not acceptable by the agreement in the project spec, and so is not a viable solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不直接将用户发送到记录点击的 ASPX 页面,然后发送
location
标头以重定向到新的 url,而不是使用 AJAX?例如,单击链接时,将发生以下情况:redirect.aspx
将处理跟踪和重定向到新 URL。这几乎是您在发送用户之前无需等待 AJAX 请求完成即可完成此操作的唯一方法。另请参阅:将用户重定向到另一个页面 (MSDN)
这可能值得请注意,您每天都会在许多网站上看到这种方法。最常见的是,它们出现在广告中。 AdSense 广告链接看起来像这样:
ish,无论如何(我把它敲下来了一点)。点击它后,您将访问 http://www.google.com/aclk,该网站会重定向跟踪点击后您将到达最终站点。
Instead of using AJAX, why don't you just send the user to an ASPX page that records the click and then sends the
location
header to redirect to the new url? For instance, on link click, the following would occur:redirect.aspx
would then handle both the tracking and the redirecting to the new URL. That's pretty much the only way you can do it without waiting for an AJAX request to complete before sending the user on their way.See also: Redirecting Users to Another Page (MSDN)
It's probably worth noting that you see this method every day on many sites. Most commonly, they are found in ads. An AdSense ad link looks like this:
ish, anyway (I knocked it down a bit). When you click it, you land at http://www.google.com/aclk, which redirects you to the end site after tracking the click.
如果上述内容确实很重要,您可以执行以下操作:
页面重定向后会有一些延迟。这可能是可接受的,也可能是不可接受的,具体取决于您的应用程序以及从用户到主机的平均延迟。
编辑:
除了下面的新评论之外,不幸的是
window.location
更改将中断您的 AJAX 请求。对于内部链接:您可能希望在用户登陆新页面时(而不是在他们单击超链接时)立即跟踪您的链接。
对于外部链接:我将使用服务器端重定向方法,如 Andy E 在另一个答案中建议。
If the above is really important, you can do the following:
There will be some latency from when the page will redirect. This may or may not be acceptable, depending on your application and on the average latency from your users to your host.
EDIT:
Further to the new comment below, unfortunately the
window.location
change will interrupt your AJAX request.For internal links: You may want to track your links as soon as the users land on the new page, instead of when they click on the hyperlink.
For external links: I would use a server side redirect method, as Andy E suggested in another answer.
使用
包装器来包装您的页面。然后,当您有 AJAX 请求时,将请求传递给包装器并在
上导航。这样做将使您能够获得完美的异步请求,同时仍然保持应用程序的响应能力。
当然,您也不会更改地址栏并有效地扼杀您的搜索引擎优化,但这是糟糕架构的代价。您的点击跟踪功能需要大约一秒钟的时间,这一事实简直令人震惊。也许您应该在服务器上的异步线程上启动它,并使用单独的线程(
System.Threading
)将数据提供给客户端。这将缓解您遇到的问题。否则,您可以拥有异步、可靠或“不一起破解”。任意选择两个。
编辑
不好的是,您还可以将 AJAX 请求作为弹出窗口运行:
这仍然不是一个好主意,但它会起作用。
Use an
<iframe>
wrapper to wrap your pages. Then, when you have an AJAX request, pass the request up to the wrapper and navigate on the<iframe>
. Doing this will give you the ability to have a perfectly asynchronous request while still maintaining app responsiveness.Granted, you're also not changing the address bar and effectively killing your SEO, but that's the tradeoff for having a bad architecture. The fact that your click tracking function takes about a second is simply appalling. Perhaps you should start it on an asynchronous thread on the server and just serve the data to the client with a separate thread (
System.Threading
). That would alleviate the problems you're experiencing.Otherwise, you can have asynchronous, reliable, or "not hacked together". Pick any two.
EDIT
My bad, you can also run your AJAX request as a popup:
It's still not a good idea, but it'll work.