Ajax Request

js 代码
 
  1. var http_request = false;  
  2.   
  3. function makeRequest(url, queryStr, methodName) {  
  4.     http_request = false;  
  5.     if (window.XMLHttpRequest) { // Mozilla, Safari,...  
  6.         http_request = new XMLHttpRequest();  
  7.         if (http_request.overrideMimeType) {  
  8.             http_request.overrideMimeType('text/xml');  
  9.         }  
  10.     } else if (window.ActiveXObject) { // IE  
  11.         try {  
  12.             http_request = new ActiveXObject("Msxml2.XMLHTTP");  
  13.         } catch (e) {  
  14.             try {  
  15.                 http_request = new ActiveXObject("Microsoft.XMLHTTP");  
  16.             } catch (e) {  
  17.             }  
  18.         }  
  19.     }  
  20.   
  21.     if (!http_request) {  
  22.         alert('Giving up :( Cannot create an XMLHTTP instance');  
  23.         return false;  
  24.     }  
  25.   
  26.     http_request.onreadystatechange = methodName;  
  27.     http_request.open('POST', url, true);  
  28.     http_request.setRequestHeader("Content-type","application/x-www-form-urlencoded");  
  29.     http_request.send(queryStr);  
  30. }  

readyState的取值如下:

  • 0 (未初始化)
  • 1 (正在装载)
  • 2 (装载完毕)
  • 3 (交互中)
  • 4 (完成)

http_request.responseText 取得 String
http_request.responseXML 取得 Xml

XMLHttpRequest 参考

Methods

MethodDescription
abort()Cancels the current request
getAllResponseHeaders()Returns the complete set of http headers as a string
getResponseHeader("headername")Returns the value of the specified http header
open("method","URL",async,"uname","pswd")Specifies the method, URL, and other optional attributes of a request

The method parameter can have a value of "GET", "POST", or "PUT" (use "GET" when requesting data and use "POST" when sending data (especially if the length of the data is greater than 512 bytes.

The URL parameter may be either a relative or complete URL.

The async parameter specifies whether the request should be handled asynchronously or not. true means that script processing carries on after the send() method, without waiting for a response. false means that the script waits for a response before continuing script processing

send(content)Sends the request
setRequestHeader("label","value")Adds a label/value pair to the http header to be sent

Properties

PropertyDescription
onreadystatechangeAn event handler for an event that fires at every state change
readyStateReturns the state of the object:

0 = uninitialized
1 = loading
2 = loaded
3 = interactive
4 = complete

responseTextReturns the response as a string
responseXMLReturns the response as XML. This property returns an XML document object, which can be examined and parsed using W3C DOM node tree methods and properties
statusReturns the status as a number (e.g. 404 for "Not Found" or 200 for "OK")
statusTextReturns the status as a string (e.g. "Not Found" or "OK")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UNIGUI的ajaxRequest是用于在Web应用程序中进行异步请求的功能。它允许您通过JavaScript代码向服务器发送请求并接收响应,而无需刷新整个页面。 使用ajaxRequest,您可以向服务器发送数据,然后在收到响应后,通过回调函数处理返回的数据。这使得您能够在不中断用户界面的情况下更新特定部分的内容。 下面是一个示例代码,展示了如何使用ajaxRequest进行异步请求: ```delphi procedure TForm1.Button1Click(Sender: TObject); var Request: TUniAjaxRequest; begin // 创建一个ajax请求 Request := TUniAjaxRequest.Create(Self); // 设置请求的URL Request.Url := 'http://example.com/api'; // 设置请求的方法(GET或POST) Request.Method := amPost; // 设置请求的参数 Request.AddParameter('param1', 'value1'); Request.AddParameter('param2', 'value2'); // 设置请求完成后的回调函数 Request.OnComplete := procedure(Response: TUniAjaxResponse) begin // 处理返回的数据 ShowMessage(Response.Content); end; // 发送请求 Request.Execute; end; ``` 在上面的示例中,我们创建了一个ajax请求,并设置了请求的URL、方法和参数。然后,我们定义了一个回调函数,在请求完成后处理返回的数据。最后,我们调用Execute方法发送请求。 请注意,上述示例仅为演示目的,并不代表完整的实现。具体使用方式可能会根据您的项目和需求有所不同。您可以根据UNIGUI的文档和示例代码进一步了解ajaxRequest的用法和功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值