6

This works on my dev machine, but not on a production server. I am trying to update some divs with ajax, but they are not updated, though other parts work fine. I am using IIS 6 on the server. When I debug this code on the server side with firebug, it does not hit any breakpoints I add to the success function.

Script:

function updateServiceInfo(nodeId) {
        var id = { id: nodeId };
        $.ajax({
            url: '/ServiceInfo/ServiceInfoPartial',
            type: 'GET',
            data: id,
            dataType: 'html',
            success: function (data) {
                $('#serviceInfoContent').html(data);
            },
    error: function (request, error) {

    }
        });
    }

Controller:

 public class ServiceInfoController : Controller
    {
        public ActionResult ServiceInfo()
        {
            return PartialView("ServiceInfo");
        }

        public ActionResult ServiceInfoPartial(string id)
        {
            return PartialView("ServiceInfoPartial");
        }
    }

Views:

serviceinfopartial

@model string
<p>
    Немає опису</p>

serviceinfo

<div id="serviceInfo">
    <div id="ContainerPanel" class="ContainerPanel">
        <div id="serviceInfoHeader" class="collapsePanelHeader">
            <div id="dvHeaderText" class="HeaderContent">
                Опис сервісу</div>
            <div id="dvArrow" class="ArrowClose">
            </div>
        </div>
        <div id="serviceInfoContent" class="serviceInfoContent">

        </div>
    </div>
</div>

The response that is returned in the console is

GET http://localhost/Managers/GetManagers?nodeId=563344 404 Not Found 42ms
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Andriy Khrystyanovich
  • 1,422
  • 3
  • 19
  • 38

2 Answers2

12

Ahhhhhhhhhhhhhh, another hardcoded url:

url: '/ServiceInfo/ServiceInfoPartial',

Never hardcode urls like this in an ASP.NET MVC application.

Always use url helpers to generate them:

url: '@Url.Action("ServiceInfoPartial", "ServiceInfo")',

or if this is in a separate javascript file where you cannot use url helpers simply use HTML5 data-* attributes on some DOM element:

<div id="serviceInfo" data-url="@Url.Action("ServiceInfoPartial", "ServiceInfo")">
...
</div>

and then in your javascript simply:

url: $('#serviceInfo').data('url'),

The reason your code doesn't work when you host it in IIS is because in IIS you are probably hosting your application in a virtual directory so the correct url is no longer /ServiceInfo/ServiceInfoPartial but is /YourAppName/ServiceInfo/ServiceInfoPartial. That's the reason why you should never hardcode any url and use helpers to generate them => it's because helpers handle this cases. Another benefit of using helpers is that if you later decide to change the layout of your routes in Global.asax you won't need to modify all your javascript files, etc... Your url managment is centralized in a single location.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Yes, my javasript code is in separate file. and url: '@Url.Action("ServiceInfoPartial", "ServiceInfo")',not work. And htis HTML5 atrribute will work in for example ie7 ? – Andriy Khrystyanovich Oct 25 '11 at 16:56
  • @andronz, yes it will work in IE7. That's what ASP.NET MVC 3 unobtrusive client validation and AJAX already uses. Even if IE7 is crap and doesn't support HTML5, jquery will happily interpret those attributes appended to DOM elements. And since IE7 has never heard of HTML5 or data-* attributes it would silently ignore them. – Darin Dimitrov Oct 25 '11 at 16:57
  • @DarinDimitrov, about what you wrote on separate js file 1. Why do you save the url in div rather than hidden field? 2. If you do so the javascript is coupled with the view, isn't it? – gdoron Oct 26 '11 at 12:46
  • @DarinDimitrov What do think about my answer (to my question). http://stackoverflow.com/questions/7902213/asp-net-mvc-razor-symbol-in-js-file/7914123#7914123 doesn't it resolve anything in a simple way? – gdoron Oct 27 '11 at 09:44
0

This worked for me, but only tested in Chrome 53:

Create some global scope variables in your .cshtml file, just be mindful of scope issues and give your variables unique names.

<script>
    globalUrl = '@Url.Action("ServiceInfoPartial", "ServiceInfo")';
</script>

Then reference your js file...

<script type="text/javascript" src="yourJsFile.js"></script>

Inside your yourJsFile.js:

url: globalUrl,
Leo Piero
  • 1
  • 1