Jot Dhaliwal
Jot Dhaliwal

Reputation: 1500

Server.Map Path method for AJAX (JQUERY)

Well ! I have One Ajax Function that is used to save innerHtml data ...

 $.ajax({
    type: "Post",
    url: "/jotform/Default.aspx/save",
    data: JSON.stringify(Senddata),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (result) {
        }
    }
});

On my local system code is inside One folder that is JOTFORM, but on my live server i have no such folder, So everytime i have to remove the /JOTFORM/ From URL. the code is inside WWWROOT folder on my server

IS there any server.mappath method to acheive the same , without editing the Ajax url element everytime.

Upvotes: 0

Views: 2209

Answers (3)

Vadim Costin
Vadim Costin

Reputation: 5

You could try a global configuration literal in a separate file, if you use GIT you probably should add file to .gitignore if it's environment dependent.

var configuration = {
    localUrl: '/jotform/Default.aspx/save',
    prodactionUrl: '/Default.aspx/save'
};

var url = configuration.localUrl || configuration.prodactionUrl;

Depends on were Your application is deployed You can add different configurations, and configurations can be easily modified.

Upvotes: 0

Guruprasad J Rao
Guruprasad J Rao

Reputation: 29683

Give url with Page.ResolveUrl as below:

$.ajax({
    type: "Post",
    url: '<%= Page.ResolveUrl("~/jotform/Default.aspx/save")%>',
    data: JSON.stringify(Senddata),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (result) {
        }
    }
});

Upvotes: 1

Brent Rittenhouse
Brent Rittenhouse

Reputation: 857

Here is one way to tackle it:

// Set the following AFTER jQuery is loaded but BEFORE your $.ajax calls.
(function($, undefined) {
    this.local = false;

    this.ajax = function(options) {
        options = options || {};

        if (options.url && this.local) {
            options.url = '/jotform' + options.url;
        }

        $.ajax(options);
    };

    window._$ = { 
        local: this.local, 
        ajax: this.ajax 
    };
})(jQuery);

_$.local = true; // or false

// . . .
// Other Code
// . . .

// Here is your original code modified. As you can see, the only changes
// are adding an underscore to the beginning and removing the /jotform prefix.
_$.ajax({
    type: "Post",
    url: "/Default.aspx/save",
    data: JSON.stringify(Senddata),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (result) { }
}); 

You would place this somewhere after jQuery is loaded but before your use of $.ajax. This will self execute and create an object called _$ which has a boolean at _$.local which you can set dynamically (or manually) to either true or false, and an alternative ajax function that simply intercepts your options, alters the URL, and passes it onward to the 'real' jQuery ajax function.

You would need to do a find a replace of your $.ajax functions (to _$.ajax) but it's probably a lot less work in the long run. This could also be modified to capture and overwrite the real jQuery ajax function but I don't really recommend that for multiple reasons.

Upvotes: 0

Related Questions