0

我知道问题有点模棱两可,但我无法用更简单的语言来描述它。

问题:我想使用带有 jquery 的 ajax 从数据库中获取一大块数据。我知道如何从数据库中获取数据并将其作为响应发送,但问题是如何在“ajax with jquery”中形成请求并获得响应。

之后,我需要在一个函数(Javascript)中传递“我们从客户端数据库中获得的内容”,该函数可以根据响应执行某些操作。

我将使用 jsp 页面发送请求。来自 ajax 的请求将转到 servlet,并且再次响应将来到同一个 jsp 页面。

4

4 回答 4

2

客户端ajax jquery调用

 $.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);
       }
  });

服务器端功能..

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

 echo json_encode($result);   //example
于 2013-01-23T10:08:01.037 回答
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

于 2013-01-23T10:06:18.640 回答
1

你可以这样做

$.ajax({
      url: url,
      data: '',
      dataType: 'json/xml', 
      success:function(res){  
          console.log(res);
       }
  });
于 2013-01-23T10:31:24.433 回答
0

Javascript代码是...

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);
}
于 2013-01-23T10:12:27.710 回答