jquery/json doesnot work after deployment of asp.net mvc application

Last post 10-29-2009 9:50 PM by ricka6. 7 replies.

Sort Posts:

  • jquery/json doesnot work after deployment of asp.net mvc application

    10-28-2009, 1:14 AM
    • Member
      point Member
    • sumit565
    • Member since 10-22-2009, 12:24 PM
    • Posts 11

    Environment used - Asp.net mvc 1.0 with c#

    Problem - I have deployed mvc application in localhost webserver and add all supporting file in webserver , and my application run successfully but Jquery/Json/getjson() method is not working. i don`t know ,what is the problem?

    anyone who know the solution,

    Please reply to me.

    thanks

    sumit

  • Re: jquery/json doesnot work after deployment of asp.net mvc application

    10-28-2009, 6:27 AM
    Answer
    • Member
      551 point Member
    • Mad-Halfling
    • Member since 12-22-2008, 10:13 AM
    • UK
    • Posts 289

    (I assume you are calling this from some AJAX script) 2 things you could check to start with:-

    1 - are you sure your URLs are getting resolved correctly in your deployed version.  I found that using a jquery call like

    $.getJSON(url, mapdata, function(data) {.....};


    I had to set the url parameter like this (for example):-

    var url = '<%= Url.Content("~/") %>JSONData/GetData';


    to access the JSONData controller / GetData action.  Try looking at your page-source and making sure that (however you do it, via regular javascript, jquery, etc) the AJAX call's URL is correct and absolute to the root of the app - one possible pitfall, as far as I have seen, with using AJAX with MVC is that you have to be careful of your URL paths, especially if you have common views (especially if you use partial views) as they may be called from different "depths" or URL.  e.g. I have a few partial views that are search popups and they may be called from <app-root>/<controller> or <app-root>/<controller>/<action> depths so they all have to make their AJAX calls with a base url of <app-root>.

     

    If you are absolutely sure that it correct, try accessing your data directly using WGET (see the bottom for it's location, if you don't have it already).  You can the use it to check the output of your code that is producing your JSON data to see if that is the problem, or if it's the code that is handling it (or an incorrect URL, as mentioned above) that is at fault.  One of the problems with using AJAX is that it isn't always that easy to find/debug any client-side scripting issues.

    If you need to send POST data, you can use something like

    wget --post-data="abc=123" http://localhost:12345/scripts/myjsondata


    to test on your dev server (this will save the raw data returned to a file) - edit the URL and port to fit your dev server's port and the URL of your JSON data provider - this will send the data "123" with a form input name of "abc" (if you need to POST).  Obviously if you are just using an http GET you can leave out the --post-data param and append any parameters to the URL with a ? - e.g. http://localhost:12345/scripts/myjsondata?searchValue=123 .  You can then do this against the deployed version to see what you get out from that, e.g.

    wget --post-data="abc=123" http://mywebsvr/myapplication/scripts/myjsondata


    and see if the data is still coming out correctly.

    I have had WGET for ages, I believe I got it from

    http://www.gnu.org/software/wget/

    or

    http://gnuwin32.sourceforge.net/packages/wget.htm

    - I am sure they are both safe and correct, but make sure you virus check any software you download from there.

    Please remember to mark replies as answers if you find them useful =8)
  • Re: jquery/json doesnot work after deployment of asp.net mvc application

    10-28-2009, 6:42 AM
    • Member
      551 point Member
    • Mad-Halfling
    • Member since 12-22-2008, 10:13 AM
    • UK
    • Posts 289

    I the above post doesn't make sense and you are using AJAX, can you post back the source code of your calling page, along with the rendered html from your development page and also your deployed page, please? 

    Please remember to mark replies as answers if you find them useful =8)
  • Re: jquery/json doesnot work after deployment of asp.net mvc application

    10-28-2009, 8:56 AM
    • Member
      point Member
    • sumit565
    • Member since 10-22-2009, 12:24 PM
    • Posts 11

    I am using following code :

    Controller code:

    [HandleError]
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                return View();
            }

    public JsonResult GetColumn(String item)
            {
                String s;
                if (item == "select")
                {
                    s = "";
                    return this.Json(s);
                }
                else
                {
                    List<Folderid> list1 = Folderid.GetFolderid().ToList();
                    return this.Json(list1);
                }
                           
            }

    }

    Class that populate item list :

    public class Folderid
        {
            public string ICPartID { get; set; }
            public string CountryName { get; set; }

            public static List<Folderid> GetFolderid()
            {
                return new List<Folderid>   
                {   
                    new Folderid {   
                        ICPartID = "DeletedItems",   
                        CountryName = "United-States"  
                    },   
                    new Folderid{   
                        ICPartID = "Inbox",   
                        CountryName = "United-States"  
                    } ,   
                    new Folderid{   
                        ICPartID = "Outbox",   
                        CountryName = "United-States"  
                    }   ,   
                    new Folderid{   
                        ICPartID = "SentItems",   
                        CountryName = "United-States"  
                    }   ,   
                    new Folderid{   
                        ICPartID = "LostandFound",   
                        CountryName = "United-States"  
                    }     
                };
            }
        }

    Global.asax code:

    public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

                routes.MapRoute(
                 "GetColumn",
                 "GetColumn/{item}",
                 new { controller = "Home", action = "GetColumn", item = "" }
                );

                routes.MapRoute(
                    "Default",                                              // Route name
                    "{controller}/{action}/{id}",                           // URL with parameters
                    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
                );

            }

            protected void Application_Start()
            {
                RegisterRoutes(RouteTable.Routes);
            }

    View (index.aspx) code:

    <asp:Content ID="aboutContent" ContentPlaceHolderID="MainContent" runat="server">


    <script type="text/javascript">

        $(document).ready(function() {

        $('#dropdownlist1name').change(function() {

        $.getJSON("Getcolumn/" + $("dropdownlist1name > option:selected").attr("value"),

                          null,

                          function(data) { addCustomers(data); });

            });

        });



        function addCustomers(data) {

                   

                // remove any existing item
                
                $('#dropdownlist2name >option').remove();
                
                var list = $("dropdownlist2name")[0];

                // Loop over the returned collection
                for (var i = 0; i < data.length; i++) {

                    var cust = data[i];

                    var option = new Option(cust.ICPartID, cust.ICPartID);

                    list.add(option);

                };      

        };   
      

    </script>


        

    <select id="dropdownlist1name" name="dropdownlist1name"><option value="select">select</option>
    <option value="senderid">senderid</option>
    </select>
    <select id="dropdownlist2name" name="dropdownlist2name" style="display:none"></select><br /><br /><br />


    </asp:Content>


    Site.master code: add all reference inside header tag

    <script type="text/javascript" src="../../Scripts/jquery-1.3.2.js"></script>
    <script type="text/javascript" src="../../Scripts/jquery-1.3.2.min-vsdoc.js"></script>
       <script type="text/javascript" src="../../Scripts/MicrosoftAjax.js"></script>
    <script type="text/javascript" src="../../Scripts/MicrosoftMvcAjax.js"></script>


    Here First Dropdownlist is static and jquery is filling second combo onchange of dropdownlist item.

    Jquery is not running when get publish on IIS.

    Please provide the solution .

    Regards

    sumit


  • Re: jquery/json doesnot work after deployment of asp.net mvc application

    10-28-2009, 2:20 PM
    Answer
    • Member
      551 point Member
    • Mad-Halfling
    • Member since 12-22-2008, 10:13 AM
    • UK
    • Posts 289

    Can you try changing the section of javascript

        $.getJSON("Getcolumn/" 

    to

        $.getJSON("<%= Url.Content("~/") %>/HomeController/Getcolumn/" 


    and first see if that works running it from VS, and if so try deploying it please?
     

    Please remember to mark replies as answers if you find them useful =8)
  • Re: jquery/json doesnot work after deployment of asp.net mvc application

    10-28-2009, 5:24 PM
    Answer
    • Contributor
      5,552 point Contributor
    • ricka6
    • Member since 06-25-2008, 6:04 PM
    • Redmond
    • Posts 910
    • AspNetTeam
      Moderator

    This is almost always from incorrectly referencing the scripts.  Most published samples get this wrong, while it works in 

    Cassini (AKA Visual Studio ASP.NET development server) - it does't work on the real server.

    MVC FAQ

    Q: How do I reference scripts?
    A: <script src="<%= Url.Content("~/Public/Scripts/RunActiveContent.js") %>" type="text/javascript"></script>
    CDN is the best approach:
    <script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.3.2.js" type="text/javascript"></script>


    Rick -ASP.Net UE MVC FAQ   Rick on MVC & Dynamic Data   
  • Re: jquery/json doesnot work after deployment of asp.net mvc application

    10-29-2009, 4:59 AM
    Answer
    • Member
      551 point Member
    • Mad-Halfling
    • Member since 12-22-2008, 10:13 AM
    • UK
    • Posts 289

    This point is an extension of what I was saying and is another easy problem to come across in general.  As a quick test I also deploy the app to a subdirectory of the document root inetpub/wwwroot on IIS on my machine (so it will be published to http://localhost/apps/<appname> for example) so I can see if the links should work ok in a deployed environment. 

    Please remember to mark replies as answers if you find them useful =8)
  • Re: jquery/json doesnot work after deployment of asp.net mvc application

    10-29-2009, 9:50 PM
    • Contributor
      5,552 point Contributor
    • ricka6
    • Member since 06-25-2008, 6:04 PM
    • Redmond
    • Posts 910
    • AspNetTeam
      Moderator

    Good idea. If people would test on IIS before they deploy, they would eliminate these problems. 

    Rick -ASP.Net UE MVC FAQ   Rick on MVC & Dynamic Data   
Page 1 of 1 (8 items)