0

I know Question is a little ambiguous,But i am unable to describe it in simpler language.

Problem: I want to get a chunk of data from database using ajax with jquery.I know how to get data from database and send it as response but Problem is how to form request in "ajax with jquery" and get the response.

After that I need to pass "what we get from Database on client-side" in a function(Javascript) which can do something depending on the response.

I will be using a jsp page to send request. Request from ajax will go to servlet, and again response will come to same jsp page.

Paddy
  • 33,309
  • 15
  • 79
  • 114

4 Answers4

2

client side ajax jquery call

 $.ajax({
      url: path/to/your/function,
      data: '',//data tobe  send to the server (optional)
      type:'post', //either post or get
      dataType: 'json', //get response as json fron server
      success:function(data){  //this function is called when the ajax function is successfully executed
            alert(data) ;  OR  console.log(data);
       }
  });

server side function..

make query to your data base... return your response as json...

 echo json_encode($result);   //example
bipen
  • 36,319
  • 9
  • 49
  • 62
  • this is (almost)what i was looking for...Thanks!! –  Jan 23 '13 at 10:13
  • is there a way to store{Like in a variable} what we have got(gotten) from server-side??? var var_name= $.ajax({ url: path/to/your/function, success:function(data){ //this function is called when the ajax function is successfully executed return data; } }); Will it work??? –  Jan 23 '13 at 10:21
  • in php we do `echo json_encode($result)` to convert the gotten data from server to json where $result is the data from server...in jsp you can have a look to this link http://stackoverflow.com/questions/3207092/what-is-the-jsp-equivalent-to-json-encode-in-php – bipen Jan 23 '13 at 10:25
  • 1
    no... the server returned datas is stored in `data` in my case ..check ajax success function.... `success:function(data){..` here... if `success:function(result){` then your server return datas is in a var call result... – bipen Jan 23 '13 at 10:29
1

your question is very hard to understand, let see

if you want consume a service to get data like database or other with jquery you can see this - Consume Service Jquery AJAX

depending on the response you can do a condition to check if data is correct or not, or get the value fields or others, i dont know if this is what you need

Ricardo Vieira
  • 730
  • 7
  • 13
  • Aha!!!...Correctly understood. I do need to put some condition-checks on client side on the data got from server.Thanks For helping with your precise time. –  Jan 23 '13 at 10:15
1

you can do this

$.ajax({
      url: url,
      data: '',
      dataType: 'json/xml', 
      success:function(res){  
          console.log(res);
       }
  });
Stefan
  • 31
  • 2
0

Javascript code is...

function ajaxProcessor(){
var XMLHttpRequestObject = false;
if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {//FOR IE
    XMLHttpRequestObject = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {//FOR ALL OTHER BROWSERS
    try {
        XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
        XMLHttpRequestObject = false;
    }
}
}
if (XMLHttpRequestObject) {

    XMLHttpRequestObject.open("POST", "YOUR URL OR ACTION FOR STRUTS USERS", true);

    XMLHttpRequestObject.setRequestHeader('Content-Type',
            'application/x-www-form-urlencoded');

}
XMLHttpRequestObject.onreadystatechange = function() {
    if (XMLHttpRequestObject.readyState == 4
            && XMLHttpRequestObject.status == 200) {

            y = XMLHttpRequestObject.responseText;

            //DO SOMETHING WITH RESPONSE HERE
        }

};
    //POSTING THE DATA 
XMLHttpRequestObject.send("VAR_NAME1=" + VALUE+ "&VAR_NAME2=" + VALUE);
}
Ram
  • 845
  • 1
  • 13
  • 26
  • Ummm..no Ram. using native library functions from Javascript...thats what i was trying to avoid. Jquery is much much better. –  Jan 23 '13 at 10:17