开发者

Ajax call has different URL in Chrome than Firefox

I have found that when making an ajax call, the URL appears to be different in chrome and in firefox.

I have the following code:

       commandUrl 开发者_如何学C= 'Demo/A/';
       $.ajax(
              {
                url: commandUrl,
                data: { id: index },
                type: "GET",
                success: function (data) {
                  $("#serverMessage").html(data);
                }
                ,
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                  alert(textStatus + " - " + errorThrown);

                }
              })

In chrome the above does NOT work and the http request that fails is of the format

http://ip:port/Demo/Demo/A?id=0

but in firefox it works with the URL : http://ip:port/Demo/A?id=0

I am using Asp.net MVC 2 (where my controller is Demo) which may explain what is happening but clearly I am not sure why the change is occurring between two different browsers.

JD


You should never hardcode urls like this. Always use URL helpers when dealing with urls:

commandUrl = '<%= Url.Action("A") %>';
$.ajax({
    url: commandUrl,
    data: { id: index },
    type: "GET",
    success: function (data) {
        $("#serverMessage").html(data);
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert(textStatus + " - " + errorThrown);
    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜