1

I am working on a MVC 3 site and everything was working until I util I upgraded to jquery1.7.1 using nuget. Now my $.getJSON calls are not making the request to the server. Fiddler does not show a request being made back to the server and code before and after the $.getJSON is executing. Any ideas would be appreciated.

Here is a sample client script:

var srvBaseUrl = "http://localhost:15627/Home/";
$.getJSON(srvBaseUrl + "GetInfo", { 'id': 1 }, function (allData) {
      alert(allData);
});

and the corresponding server code:

[HttpGet]
public ActionResult GetInfo(int id)
{
    return Json(id, JsonRequestBehavior.AllowGet);
}

I tested that the server code works building up the request in Fiddler.

5
  • @MarekKarbarz no errors. Fiddler and firebug both show that no request is even being made. Commented Feb 20, 2012 at 1:32
  • How are you triggering the Ajax call? Could you post some code? Could be the case that there were some breaking changes between 1.5.1 and 1.7.1 that messed up your code. Commented Feb 20, 2012 at 1:35
  • Edit your question to include the view code where the jQuery is wired up. It will help get an answer since there is not currently enough information. Commented Feb 20, 2012 at 2:06
  • Do you execute this code on page load? Button click? If you put an alert between var srvBaseUrl and $.getJSON does it get executed? Commented Feb 20, 2012 at 2:12
  • @MarekKarbarz yes, I tried with alerts in there and it is called from:$(function () {//call my function here.}); Commented Feb 20, 2012 at 2:21

2 Answers 2

2

I had a similar problem, it turned out that JQuery was caching the results. This would explain your comments above that it started working after you made a change.

If you have a look at http://api.jquery.com/jQuery.getJSON/, you will see that $.getJSON is just shorthand for

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback
});

If you use the $.ajax instead of the shorthand $.getJSON you can disable caching using the cache parameter, meaning you should be able to replace your code with:

$.ajax({
  url: srvBaseUrl + "GetInfo",
  dataType: 'json',
  data: { 'id': 1 },
  success: function (allData) { alert(allData); },
  cache: false
});
Sign up to request clarification or add additional context in comments.

Comments

1

Nuget is not going to update your Views/Shared/_Layout.cshtml file with the updated reference to the latest version of jQuery installed. You will need to update this yourself:

<script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"> </script>

EDIT

Assuming that is all in order like you said, make sure your getJSON call is inside a document ready function as it work as-is for me. I would however change how you are building the Url to use the helper method instead:

<script type="text/javascript">
    (function ($) {
        $.getJSON('@Url.Action("GetInfo")', { 'id': 1 }, function (allData) {
            alert(allData);
        });
    })(jQuery);
</script>

This tested out fine in a stock ASP.NET MVC3 Internet Application template project using your controller action as-is.

6 Comments

yep, I thought of that and did update. Should have included that in my original notes.
@DonD What version of jQuery did you upgrade from?
the version that was installed with MVC3 which is 1.5.1
Thanks @tawman. That is what I was doing. I've added this line jQuery.support.cors = true; and it started working. So, I commented it back out and it still worked. I wonder if the ASP.Net Development server got in an odd state. I did reboot since trying to figure this out.
@DonD I hate those kind of problems... a couple hours you will never get back. Glad it sorted itself out.
|

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.