ajax call wrong url

Last post 08-20-2009 2:06 PM by maverickhyd. 2 replies.

Sort Posts:

  • ajax call wrong url

    08-20-2009, 7:51 AM
    • Member
      2 point Member
    • coosha
    • Member since 08-20-2009, 11:49 AM
    • Posts 1

    Hi.

    I have wrong URL in my ajax calls.

    $.ajax({
      type: "POST",
      url: "Home/GetDetails", ......});
     
    HomeController has action GetDetails().

    All works fine, when I load page with URL http://localhost/projectName. Ajax URL is  http://localhost/projectName/Home/GetDetails
    But after loading http://localhost/projectName/Home/Index all my ajax calls are going to http://localhost/projectName/Home/Home/GetDetails and thats wrong.

    Please, how can I solve this?

    Filed under: , ,
  • Re: ajax call wrong url

    08-20-2009, 9:29 AM
    Answer
    • Member
      100 point Member
    • leather
    • Member since 09-23-2005, 12:52 PM
    • Posts 22

    This looks to me like a Jquery issue but anyhow... you need to use the Url object to generate a relative Url. In the preview this had problems in that it didn't pick up the ajax context, but it does now.

    One way would be to put something like this into you script:

    var myUrl = '<%= Url.Action("GetDetails","Home"); %>';

    $.ajax({
      type: "POST",
      url: myUrl });

  • Re: ajax call wrong url

    08-20-2009, 2:06 PM
    Answer
    • Contributor
      2,387 point Contributor
    • maverickhyd
    • Member since 03-25-2009, 2:38 AM
    • Posts 412

    Hi,

    coosha:
    url: "Home/GetDetails"
    --slash is missing here

    You can Pass URL as

    url:"/Home/GetDetails"(with Slash before Home)

    or you can send by using url.Action as mentioned in above Post

    var urlAction = "<%= Url.Action("GetDetails", "Home") %>";

    Please Mark as Answer if it helped You!
Page 1 of 1 (3 items)