MrM
MrM

Reputation: 21989

Json path triggers local, but not when published. Why?

I have a .js file that works fine locally, but I am somehow having path problems when I commit and add to my server. I have "Home" for the controller name, and "LoadTable" for the JsonResult function.

$(document).ready(function () {
$('#tableClick').click(function () {
    $.post("Home/LoadTable", $('#FormTable').serialize(), function (data) {
        alert("test");
    }, 'json');
});
});

Should I have the path name be something else when published? If so how do I toggle from local to server version in my webconfig, if possible?

Is there another option?

Upvotes: 0

Views: 221

Answers (3)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

Never hardcode urls. Always use Url helpers when dealing with urls:

$.post(
    '<%= Url.Action("LoadTable", "Home") %>', 
    $('#FormTable').serialize(), 
    function (data) {
        alert('test');
    }, 
    'json'
);

Another possibility is to have a link somewhere on your page:

<%= Html.ActionLink("Load table", "LoadTable", "Home", null, new { @class = "loadTable" }) %>

which you would AJAXify:

$(function() {
    $('.loadTable').click(function() {
        $.post(this.href, $('#FormTable').serialize(), function (data) {
            alert('test');
        }, 'json');
        return false;
    });
});

Upvotes: 0

MrM
MrM

Reputation: 21989

With Val's help, I was able to come up with the following.

var pathName = window.location.pathname;
if (pathName == "/") { pathName = ""; }
$.post(pathName + "Home/LoadTable", $('#FormTable').serialize(), function (data) { 
    alert("test");
}, 'json');    

Upvotes: 0

Val
Val

Reputation: 17522

  1. /Home/LoadTable = absolute = example.com/Home/LoadTable
  2. Home/LoadTable = relative = {current_url}/HomeLoadTable

Upvotes: 2

Related Questions