0

I tried to make sense of the results I got (see ajax call below) in relation to this post:

Relative URLs in AJAX requests

The comments annotated with [WORKS and [FAILS] below show various URLs that worked and failed. Since the above post only talked about document based URLs, I tried to make the analogy that a method in a controller corresponds to a document and a controller to folder. However, the results below belie that analogy.

CAS is a MVC application which runs off the root of a website. Subject is a controller. Index, GetSubject and RegisterSubject are methods in the Subject controller.

Not all of these URLs should work. However, I cannot explain why one does another does not.

EDIT: UrlHelpers. I want to understand the rules. What are the rules for resolving a MVC type of url: http://[domain]/[MVC App]/[Controller]/[Method]/ [params] where params is like [p1/p2/ . . ].

I don't see the pattern. As noted below the document url = [http://localhost/cas/Subject/Index] when either method is called.

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;
    };
}
Community
  • 1
  • 1

0 Answers0