Friday, December 13, 2013

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?
share|improve this question
add comment

7 Answers

$.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);
    });
}
share|improve this answer
add comment
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 ofreq.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);
share|improve this answer
add comment
I don't think jQuery implements a timeout function, but plain old javascript does it rather nicely :)
share|improve this answer
add comment
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.
share|improve this answer
 
what goes in data? –  Bernie Perez Sep 19 '08 at 18:32
 
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 Sep 19 '08 at 18:33
 
"data" is key/value pairs that will be sent the server. Also, the examples at docs.jquery.com/Ajax/jQuery.getshow calls that omit the data parameter. –  Blair Conrad Sep 19 '08 at 18:34
 
hmm... I tried url = "/My_Servlet/response"; jQuery.get(url, "key=" + escape(key.value), callback); still wont call the callback function. –  Bernie Perez Sep 19 '08 at 18:38
 
and I tried null for data as well. =( –  Bernie Perez Sep 19 '08 at 18:39
show 3 more comments
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;
});
share|improve this answer
add comment
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.
share|improve this answer
add comment

5 comments:

oakleyses said...

louis vuitton handbags, oakley sunglasses, louboutin, longchamp outlet, nike shoes, louis vuitton outlet stores, chanel handbags, burberry outlet, prada outlet, jordan shoes, tiffany and co, michael kors outlet, tory burch outlet, louis vuitton outlet, longchamp handbags, nike free, true religion jeans, michael kors outlet, kate spade outlet, polo ralph lauren outlet, tiffany and co, prada handbags, polo ralph lauren outlet, michael kors outlet, michael kors outlet, longchamp handbags, oakley sunglasses, ray ban sunglasses, kate spade handbags, burberry outlet, louis vuitton outlet, louboutin outlet, louboutin, coach factory outlet, air max, air max, coach outlet, gucci outlet, christian louboutin shoes, michael kors outlet, coach purses, ray ban sunglasses, michael kors outlet, louis vuitton, coach outlet store online, true religion jeans, oakley sunglasses cheap

oakleyses said...

ralph lauren, lululemon, air max, hollister, north face, nike air max, polo lacoste, vanessa bruno, timberland, vans pas cher, louboutin, louis vuitton, oakley pas cher, air max pas cher, nike roshe run, air max, true religion outlet, barbour, sac longchamp, air force, hollister, sac louis vuitton, nike free, polo ralph lauren, nike trainers, louis vuitton uk, nike roshe, sac hermes, longchamp, michael kors, sac burberry, sac guess, mulberry, new balance pas cher, converse pas cher, sac louis vuitton, hogan outlet, nike tn, north face, true religion outlet, ray ban pas cher, michael kors, air jordan, nike blazer, nike free pas cher, michael kors pas cher, abercrombie and fitch, ray ban sunglasses

oakleyses said...

mac cosmetics, mont blanc, marc jacobs, canada goose outlet, nike huarache, vans shoes, soccer jerseys, hollister, giuseppe zanotti, beats by dre, abercrombie and fitch, longchamp, insanity workout, celine handbags, bottega veneta, ghd, nfl jerseys, north face outlet, chi flat iron, ugg boots, birkin bag, ugg australia, canada goose, herve leger, ugg pas cher, rolex watches, valentino shoes, canada goose uk, canada goose, ferragamo shoes, canada goose, ugg boots, uggs outlet, north face jackets, soccer shoes, asics running shoes, new balance shoes, p90x, lululemon outlet, canada goose jackets, mcm handbags, instyler, babyliss pro, ugg, wedding dresses, jimmy choo outlet, reebok outlet, nike roshe run

oakleyses said...

parajumpers, karen millen, air max, converse, pandora charms, moncler, louboutin, moncler, links of london, lancel, juicy couture outlet, oakley, hollister, pandora charms, supra shoes, thomas sabo, canada goose, gucci, wedding dresses, timberland boots, swarovski crystal, air max, coach outlet store online, moncler, ray ban, canada goose, moncler, ugg, louis vuitton, swarovski, hollister, montre homme, moncler, hollister clothing store, ralph lauren, rolex watches, moncler outlet, moncler, iphone 6 cases, baseball bats, juicy couture outlet, toms shoes, vans, pandora jewelry, ugg, converse shoes

Zheng junxai5 said...

zhengjx20160721
nike roshe run
coach outlet
adidas yeezy
kobe shoes 11
louis vuitton bags
red bottom shoes
adidas running shoes
cheap jordan shoes
coach factory outlet
louis vuitton outlet
michael kors handbags
louis vuitton handbags
michael kors outlet
adidas outlet store
retro 11
nike outlet
instyler
adidas yeezy
coach outlet online
ray bans
coach factory outlet online
hermes birkin handbags
insanity workout
adidas wings
toms shoes
air jordan shoes
nike trainers women
burberry handbags
ralph lauren outlet
oakley sunglasses wholesale
nike roshe one
cheap rolex watches
louis vuitton outlet
michael kors outlet clearance
cheap ray ban sunglasses
nike store
cheap jerseys
adidas factory outlet
tiffany and co
ray ban outlet