2

I have a post that work well when I run from VS2015 debug:

        $("#DisplayChartType").bind("change", function () {
            $.post("../Employee/ChangeDisplayChartType", { displayChartType: $("#DisplayChartType").val() }, function (data) {
                iDependOnMyParameter(data);
            })
        });

But post does not work once I have published to IIS. I tried using ../, / and ~/ in the post but none work. I searched web and found the approach below but I still get ARG1 being sent as a parameter instead of my javascript variable.

        $("#DisplayChartType").bind("change", function () {
            $.post("@Html.Action("ChangeDisplayChartType", "Employee", new { displayChartType = "ARG1" })".replace("ARG1",$("#DisplayChartType").val()) , function (data) {
                iDependOnMyParameter(data);
            })
        });

How should I do this? I really would like to stay with $.post approach as that works nicely in VS.

3
  • Seems weird to use post and use a querystring. If you switched to GET, it will be added to the querystring. Commented Nov 16, 2016 at 13:38
  • instead of trying this workaround I would investigate why your code is not working once published in IIS. Can you check the address called by the post? Commented Nov 16, 2016 at 13:45
  • bind is deprecated. You should use on instead. Commented Nov 16, 2016 at 14:05

4 Answers 4

1

You can try this code.

  $("#DisplayChartType").bind("change", function () {
        var chartType = $("#DisplayChartType").val();
        var url="@Url.Action("ChangeDisplayChartType", "Employee", new { displayChartType = "ARG1" })";
        $.post(url.replace("ARG1", chartType), function (data) {
            iDependOnMyParameter(data);
        })
    });
Sign up to request clarification or add additional context in comments.

Comments

0

So add it to the url

$.post("../Employee/ChangeDisplayChartType?displayChartType=" + encodeURIComponent($("#DisplayChartType").val()), function(){});

or change your original code to GET and the value will be added to the querystring.

3 Comments

Like I put in question ../Employee or /Employee or ~/Employee do NOT work when published to IIS
$.post("@Html.Action("ChangeDisplayChartType", "Employee", new { displayChartType = "ARG1" })" + "?displayChartType="" + encodeURIComponent...
Or look at what that produces when you view the source and use the same thing in your post....
0

You can use window.location.origin or document.location.origin to get the origin of your website, whether running in VS 2015 debug or on IIS.

So instead of doing

$.post("../Employee/ChangeDisplayChartType"

You can try

$.post(document.location.origin + "/Employee/ChangeDisplayChartType"

1 Comment

Almost! See my answer please
0

@OJ Raqueno put me on the right path.

At top of script I now declare "myPath". My website URL ends with "secure" so this test gives me the right path:

    var myPath = document.URL;
    if (!myPath.endsWith("secure")) {
        myPath = "";
    }

Then I do this:

    $("#DisplayChartType").bind("change", function () {

        $.post(myPath + "/Employee/ChangeDisplayChartType", { displayChartType: $("#DisplayChartType").val() }, function (data) {
            alert($("#DisplayChartType").val());
            iDependOnMyParameter(data);
        })
    });

1 Comment

Glad to know I helped. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.