简体   繁体   English

jQuery AJAX 中的正斜杠“/”问题

[英]Problem with forward slash “/” in jQuery AJAX

I always wrote URLs used by AJAX calls in this way: "/Home/Save" with the forward slash in the beginning.我总是以这种方式编写 AJAX 调用使用的 URL: "/Home/Save" ,开头带有正斜杠。 Now this last project is being deployed to a virtual directory on a server.现在,这最后一个项目正在部署到服务器上的虚拟目录中。 Thus, these URLs aren't working anymore, because instead of "example.com/VirtualDir/Home/Save" , they would point to "example.com/Home/Save" which is wrong.因此,这些 URL 不再起作用,因为它们会指向错误的"example.com/Home/Save" example.com/Home/Save”而不是"example.com/VirtualDir/Home/Save" I quickly fixed the problem by removing the first forward slash "/" in all occurrences of URLs in my JavaScript.我通过删除 JavaScript 中所有出现的 URL 中的第一个正斜杠"/"快速解决了这个问题。 All pages work great, except for one, When AJAX call happens on the problematic page.所有页面都运行良好,除了一个,当 AJAX 调用发生在有问题的页面上时。 the specified URL gets appended to the page URL, I've spent a few hours yesterday and the whole morning today.指定的 URL 附加到页面 URL,我昨天花了几个小时,今天花了整个上午。 and I cannot figure it out.我想不通。 There is absolutely nothing different about this page comparing to others?与其他页面相比,此页面绝对没有什么不同? Has anyone had this problem before?以前有人遇到过这个问题吗? Should I post my code?我应该发布我的代码吗?

EDIT: After banging my head on the keyboard for another few hours, I ended up implementing the following.编辑:在键盘上敲了几个小时后,我最终实现了以下内容。 I got an action in a common Controller that returns the result of Request.Url.GetLeftPart(UriPartial.Authority) , which is your http://www.mysite.com . I got an action in a common Controller that returns the result of Request.Url.GetLeftPart(UriPartial.Authority) , which is your http://www.mysite.com . I render it inside my Layout page into a global JavaScript variable, _AppPath .我将它在我的布局页面中呈现为全局 JavaScript 变量_AppPath Then, every AJAX call gets its URL like this: _AppPath + '/Controller/Action' .然后,每个 AJAX 调用都会得到它的 URL ,如下所示: _AppPath + '/Controller/Action' This works everywhere and I still don't know what the hack is the problem with that page.这在任何地方都有效,但我仍然不知道该页面的问题是什么。 Cheers!干杯!

Can you change the Ajax requests so that they instead point to "/VirtualDir/Home/Save"?您能否更改 Ajax 请求,使它们指向“/VirtualDir/Home/Save”?

If it helps your code, you could have a path variable, so that you can easily update the virtual directory path (or remove it) when you deploy it somewhere else.如果它对您的代码有帮助,您可以拥有一个path变量,以便在将虚拟目录路径部署到其他地方时轻松更新(或删除它)。 Or your code could read its location via the window.location.href property and work out things out from there.或者您的代码可以通过window.location.href属性读取它的位置并从那里解决问题。

It's not so useful to have paths relative to the current document (ie without the / slash prefix) because, as you are observing, some of the pages will then fail their requests, when those pages are at a different point in the site hierarchy.拥有相对于当前文档的路径(即没有 / 斜杠前缀)并不是那么有用,因为正如您所观察到的,当这些页面位于站点层次结构中的不同点时,某些页面将失败其请求。 An absolute URL would be the one to go for (ie with a / slash prefix).绝对 URL 将是 go 的一个(即带有 / 斜杠前缀)。

[UPDATED, based on comments below] [更新,基于以下评论]

@Dimskiy, it doesn't so much matter that the server-side framework is .NET MVC, or that there are no actual folders for those URLs on the server. @Dimskiy,服务器端框架是 .NET MVC 或者服务器上没有这些 URL 的实际文件夹并不重要。 The browser will just respond according to the URL structure it sees.浏览器只会根据它看到的 URL 结构做出响应。

So the things to look for are the URLs in the browser address bar for the different pages, and the URLs of the Ajax requests being made to the server (eg look for these in Firebug's "Net" panel).因此,要查找的是浏览器地址栏中不同页面的 URL,以及向服务器发出的 Ajax 请求的 URL(例如,在 Firebug 的“网络”面板中查找这些)。 And compare the URLs, looking at the number of folders suggested by each URL.并比较 URL,查看每个 URL 建议的文件夹数量。

It doesn't matter if there isn't an actual folder on the server.如果服务器上没有实际的文件夹也没关系。 The browser can't tell, it can only look at the URL structure.浏览器看不出来,只能看URL结构。 If the JavaScript is making a call from a page called "foo" to an Ajax resources at "Home/Save", then the request will be routed to "foo/Home/Save".如果 JavaScript 正在从名为“foo”的页面调用“Home/Save”处的 Ajax 资源,则请求将被路由到“foo/Home/Save”。 And if the request is made from page "foo/bar" then it will be routed to "foo/bar/Home/Save".如果请求是从页面“foo/bar”发出的,那么它将被路由到“foo/bar/Home/Save”。 That's a relative path - it's relative to the containing HTML document.这是一个相对路径 - 它相对于包含 HTML 文档。

A request to an "absolute" path, say, "/Home/Save" (note the / slash prefix) will always go to the root of the domain, eg example.com/Home/Save.对“绝对”路径的请求,例如“/Home/Save”(注意 / 斜杠前缀)将始终 go 到域的根目录,例如 example.com/Home/Save。 But since you need your request to go to the "VirtualDir" virtual directory, then your URL will become "/VirtualDir/Home/Save".但是由于您需要将 go 请求到“VirtualDir”虚拟目录,那么您的 URL 将变为“/VirtualDir/Home/Save”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM