views:

10842

answers:

7

I am kind of new to JavaScript library's. I wanted to replace my current code with a JS lib jQuery. My current code looks like this.

  var req; 
  function createRequest(){ 
    var key = document.getElementById("key"); 
    var keypressed = document.getElementById("keypressed"); 
    keypressed.value = key.value; 
    var url = "/My_Servlet/response?key="+ escape(key.value); 
    if (window.XMLHttpRequest){ 
      req = new XMLHttpRequest(); 
    } 
    else if (window.ActiveXObject){ 
      req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    req.open("Get",url,true); 
    req.onreadystatechange = callback; 
    req.send(null);
  } 

  function callback(){
    if (req.readyState==4){ 
      if (req.status == 200){ 
        var decimal = document.getElementById('decimal'); 
        decimal.value = req.responseText; 
      } 
    }
    clear();
  }

I wanted to replace my code with something a little friendlier like jQuery's

jQuery.get(url, callback);

However I it doesn't call my callback function.

Also I would like to call this function createRequest to be called continuously. Does jQuery have a nice way of doing that?

+2  A: 

I don't think jQuery implements a timeout function, but plain old javascript does it rather nicely :)

Wes P
+2  A: 

According to the docs, jQuery.get's arguments are url, data, callback, not url, callback.

A call to JavaScript's setTimeout function at the end of your callback function should suffice to get this to continually execute.

ceejayoz
what goes in data?
Whatever you like. Use NULL if you don't need any passed along, or you could potentially move key=? from the query string into the data arguments. See the docs for example code.
ceejayoz
"data" is key/value pairs that will be sent the server. Also, the examples at http://docs.jquery.com/Ajax/jQuery.get show calls that omit the data parameter.
Blair Conrad
hmm... I triedurl = "/My_Servlet/response";jQuery.get(url, "key=" + escape(key.value), callback);still wont call the callback function.
and I tried null for data as well. =(
Try {} for data.
_Lasar
If `callback` is undefined and `data` is a function, jQuery will swap the parameters automatically.
John Millikin
setTimeout() calls a function once. setInterval() is the correct function to use when you want something called again and again.
Jim
+12  A: 
$.get(url, {}, callback);

should do the trick. Your callback could be simplified like this:

function callback(content){
    $('#decimal').val(content);
}

Or even shorter:

$.get(url, {}, function(content){
    $('#decimal').val(content);
});

And all in all I think this should work:

function createRequest() {
    var keyValue = $('#key').val();
    $('#keypressed').val(keyValue);
    var url = "/My_Servlet/response";
    $.get(url, {key: keyValue}, function(content){
        $('#decimal').val(content);
    });
}
_Lasar
+3  A: 

Take out the readyState and status checks. jQuery only calls your callback upon success. Your callback is supplied the arguments (data, textStatus), so you should use data instead of req.responseText.

window.setTimeout() as suggested by another answer won't do what you want - that only waits and then calls your function once. You need to use window.setInterval() instead, which will call your function periodically until you cancel it.

So, in summary:

var interval = 500; /* Milliseconds between requests. */
window.setInterval(function() {
    var val = $("#key").val();
    $("#keypressed").val(val);
    $.get("/My_Servlet/response", { "key": val }, function(data, textStatus) {
        $("#decimal").val(data);
    });
}), interval);
Jim
+1  A: 

There's no need to set the GET parameters on the URL, jQuery will set them automatically. Try this code:

var key = document.getElementById("key");
[...]
var url = "/My_Servlet/response";
$.get (url, {'key': key}, function (responseText)
{
    var decimal = document.getElementById ('decimal'); 
    decimal.value = responseText;
});
John Millikin
A: 

Hey Everyone... I have this code... Thanks so much for all the help. But I get an error responseText is undefined on the jQuery get call.

  function convertToDecimal(){ 
    var key = document.getElementById("key"); 
    var keypressed = document.getElementById("keypressed"); 
    keypressed.value = key.value; 
    var url = "/Minesweeper_Servlet/response"; 
    $.get(url, {'key': key}, callback(responseText));
  }

  function callback(responseText) {
    alert("Here");
    var decimal = document.getElementById ('decimal'); 
    decimal.value = responseText;
    clear();
  }
Edit this into the question, with "---" between the old and new section. That way we can follow your progress more easily.
John Millikin
As for the problem, the line should be "$.get(url, {'key': key}, callback);" instead.
John Millikin
You are calling your callback and passing the result to $.get(). You need to just pass the callback as John describes. jQuery will call it for you.
Jim
A: 

In the end I guess it was added the type. This seems to work for me.

  function convertToDecimal(){ 
    var key = document.getElementById("key"); 
    var keypressed = document.getElementById("keypressed"); 
    keypressed.value = key.value; 
    var url = "/My_Servlet/response?key="+ escape(key.value);
    jQuery.get(url, {}, function(data){
       callback(data);}
       , "text" );
  }

  function callback(data){
    var decimal = document.getElementById('decimal');
    decimal.value = data;
    clear();
  }

Thanks Everyone for the help. I'll vote you up.

Bernie Perez