0

I really have no idea whats wrong with the following code . I am not getting any response in the status ...

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js">    
</script>
<input type="text" name="UserName" id="UserName" />
<div id="divStatus"></div>
<script type="text/javascript">
    $("#UserName").keyup(function() {
        var name = $("#UserName").val();
        var status = $("#divStatus");
        var user = $.trim(name);
        if (user.length > 3) {
            status.html("Checking....")
            $.getJSON("http://192.168.0.14/openids/api/json/user/username/availability/check/", {
                username: name,
            }, function(data) {
                status.html("got")
            });

        } else {
            status.html("Min 3 letters ");
        }
    });​
</script>

The json response is as follows :

  {"status": "Error", "message": "A user with this username already exists."}
adeneo
  • 312,895
  • 29
  • 395
  • 388
Captain Barbossa
  • 1,067
  • 10
  • 16

3 Answers3

3

Quoting the documentation:

If the URL includes the string "callback=?" (or similar, as defined by the server-side API), the request is treated as JSONP instead. See the discussion of the jsonp data type in $.ajax() for more details.

Since you're making a cross domain request, you probably want to use JSONP. To do this, add a callback parameter at the end of your querystring:

$.getJSON("http://192.168.0.14/openids/api/json/user/username/availability/check/?callback=", {
    username: name,
}, function(data) {
    status.html("got")
});
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
  • Thanks man ... but the following url worked ... [http://]192.168.0.14/openids/api/json/user/username/availability/check/?callback=? – Captain Barbossa Dec 17 '12 at 11:48
1

Ok, so cross-domain ajax security issue is the trouble. Your calling script and target script should be in the same domain for it to work without additional effort. If you open your browser's error console you should be able to see a security exception there.

Unless you've explicitly set it up to support requests from other hosts.

Check these answers:

How to enable cross-domain request on the server?

jQuery AJAX cross domain

Community
  • 1
  • 1
techfoobar
  • 65,616
  • 14
  • 114
  • 135
  • 1
    yup .. you are correct .. I got "XMLHttpRequest cannot load 192.168.0.14/openids/api/json/user/username/availability/check/?username=hjmghj. Origin 192.168.0.8 is not allowed by Access-Control-Allow-Origin." – Captain Barbossa Dec 17 '12 at 11:40
1

I think what you're running into is that the call is returning an error status but the .getJSON api only gives you the option for a success handler.

What you're going to need to do is look at the .ajax API, which allows you to add an error handler. The .getJSON API shows what it is shorthand for. Look at the .ajax API for all of the options you can pass in addition.

EDIT I didn't notice that this never got to the handler because this was a CORS type request.

Dave G
  • 9,639
  • 36
  • 41