-1
$(function () {
    $('input#UserName').blur(function () {
        var username = $('input#UserName').val();
        $.ajax(
        {
            type: "POST",
            url: "Profile/CheckAvailability",
            data: "username=" + username,               
            success: function (result) {
                //Some code here
            },
            error: function () {
                alert("Sorry! We could not receive your feedback at this time.");                
            }
        });
    });
});

and the code on Profile controller

    [HttpPost]
    public JsonResult CheckAvailability(string username)
    {
        bool isAvailable = false;
        //Some code here
        return Json(new {success = isAvailable});
    }

Each time alert("Sorry! We could not receive your feedback at this time."); is being triggered.

Thanks.

eomeroff
  • 9,599
  • 30
  • 97
  • 138
  • 1
    You have firebug installed? Check out the console tab in it when making this request. What is dataType JsonResult? I looked in to the jquery doc and nothing about that. Available types: xml, json, jsonp, html, text. If the retrieved data json type, then set the dataType : "json". – Mārtiņš Briedis Feb 12 '11 at 01:05
  • I have firebug, I will try in a moment ignore that dataType it was just wrong paste, It definitely does not work with out that. – eomeroff Feb 12 '11 at 01:09
  • Firebug is great - another tool I use all the time is [Fiddler](http://www.fiddler2.com/fiddler2/). It'll intercept all HTTP traffic to and from your machine, so it'll work with any browser. I've found it to be a little more full-featured than Firebug's HTTP debugger, as well. – Ryan Mitchell Feb 12 '11 at 01:11
  • Have you tried `data: {"username" : username}`? – LukLed Feb 12 '11 at 01:12
  • @LukLed: I have just did thanks. – eomeroff Feb 12 '11 at 01:18
  • The problem is that it is trying to access Profile/Profile/CheckAvailability method on controller, that is what firebug console show. How can that be possible?? – eomeroff Feb 12 '11 at 01:19

2 Answers2

1

Here are a few tips:

  1. Make sure your controller action is not throwing an exception (especially the part where you say //Some code here). This could be verified by stepping through it and debugging.
  2. Make sure to always URL encode your request parameters and never use string concatenation:

    data: { username: username },
    
  3. Never hardocde urls in your javascript file. Always use Url helpers when dealing with urls (if this is a separate javascript then you could use a global js variable set in your view):

    url: '<%= Url.Action("Profile", "CheckAvailability") %>',
    
  4. Use FireBug to see what is being sent to and received from the server.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

Try setting dataType as "json", because the one you specified isn't valid. Valid dataTypes from jquery documentation: xml, json, jsonp, html, text.

Check out this url about ajax error handling: jQuery Ajax error handling, show custom exception messages

Community
  • 1
  • 1
Mārtiņš Briedis
  • 17,396
  • 5
  • 54
  • 76