我试图理解我得到的与这篇文章相关的结果(见下面的 ajax 调用):
下面用 [WORKS 和 [FAILS] 注释的评论显示了各种有效和失败的 URL。由于上面的帖子只讨论了基于文档的 URL,我试图类比控制器中的方法对应于文档,控制器对应于文件夹。然而,下面的结果证明了这个类比。
CAS 是一个在网站根目录下运行的 MVC 应用程序。主体是控制器。Index、GetSubject 和 RegisterSubject 是 Subject 控制器中的方法。
并非所有这些 URL 都应该有效。但是,我无法解释为什么一个人不这样做。
编辑:UrlHelpers。我想了解规则。解析 MVC 类型 url 的规则是什么: http://[domain]/[MVC App]/[Controller]/[Method]/ [params] 其中 params 类似于 [p1/p2/ 。. ]。
我没有看到模式。如下所述,当调用任一方法时,文档 url = [ http://localhost/cas/Subject/Index] 。
var ajaxService = (function () {
var ajaxGetJson = function (method, jsonIn, callback, errorCallback, controller) {
var ajaxUrl = getSvcUrl(method, controller);
var docUrl = document.URL; // at his point document url = http://localhost/cas/Subject/Index
//[WORKS] when ajaxUrl = ../GetSubject/359143 where 359143 is the id of Subject in database
// correctly resolves to http://localhost/cas/Subject/GetSubject/359143
//[FAILS] when ajaxUrl = ../RegisterSubject
// resolves to http://localhost/cas/RegisterSubject <-- notice Subject controller is missing.
//[FAILS] when ajaxUrl = /RegisterSubject --> resolves to http://localhost/RegisterSubject
//[WORKS] when ajaxUrl = "RegisterSubject" or "RegisterSubject/
// resovles to http://localhost/cas/Subject/RegisterSubject
//[FAILS] when ajaxURL = "GetSubject"/359143"
// resolves to http://localhost/cas/Subject/Index/GetSubject/359143
if (method === "RegisterSubject") { //workaround for the above failure
ajaxUrl = "http://localhost/cas/Subject/RegisterSubject";
}
$.ajax({
url: ajaxUrl, //getSvcUrl(method, controller),
type: "GET",
data: ko.toJSON(jsonIn),
dataType: "json",
contentType: "application/json",
success: function (json) {
callback(json);
},
error: function(json) {
errorCallback(json);
}});
}
// other AJAX types omitted
return {
ajaxGetJson: ajaxGetJson;
};
}