AJAX Response() 方法 | Prototype教程

返回到:Prototype AJAX 教程

此 AJAX Ajax.Response是作为所有 Ajax 请求回调的第一个参数传递的对象。

这是本机 xmlHttpRequest 对象的包装器。它规范了跨浏览器问题,同时通过 responseJSON 和 headerJSON 属性添加了对 JSON 的支持。

Ajax.Response 对象的属性

Property类型描述
statusNumber服务器发送的 HTTP 状态代码。
statusTextString服务器发送的 HTTP 状态文本。
readyStateNumber请求的当前状态。0 对应“未初始化”,1 对应“加载”,2 对应“加载”,3 对应“交互”,4 对应“完成”。
responseTextString响应的文本正文。
responseXMLdocument Object
or null
如果请求的内容类型设置为 application/xml,则为响应的 XML 正文。否则为空。
responseJSONObject, Array
or null
如果请求的内容类型设置为 application/json,则为响应的 JSON 正文。否则为空。
headerJSONObject, Array
or null
X-JSON 标头的自动评估内容(如果存在)。否则为空。这对于传输少量数据很有用。
requestObject请求对象本身(Ajax.Request 或 Ajax.Updater 的实例)。
transportObject本机 xmlHttpRequest 对象本身。

例子

以下是显示status和 responseText属性用法的示例:

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "https://cdn.bootcdn.net/ajax/libs/prototype/1.7.3/prototype.min.js"></script>
      
      <script>
         function SubmitRequest() {
            new Ajax.Request('/cgi-bin/ajax.cgi', {
               method: 'get',
               onSuccess: successFunc,
               onFailure:  failureFunc
            });
         }
         function successFunc(response) {
            if (200 == response.status) {
               alert("Call is success");
            }
            var container = $('notice');
            var content = response.responseText;
            container.update(content);
         }
         function failureFunc(response) {
            alert("Call is failed" );
         }
      </script>
   </head>

   <body>
      <p>Click submit button to see how current notice changes.</p>
      <br />
 
      <div id = "notice">Current Notice</div>
      <br />
      <br />
      <input type = "button" value = "Submit" onclick = "SubmitRequest();"/>
   </body>
</html>

这是ajax.cgi的内容:

#!/usr/bin/perl

print "Content-type: text/html\n\n";

print "This content is returned by AJAX cgi 
";
print "Current Time " . localtime;

Ajax.Response 对象的方法

MethodTypeDescription
getHeader(name)String ornull如果存在,则返回所请求标头的值。否则为空。
getAllHeaders()String ornull返回一个字符串,其中包含由换行符分隔的所有标题。
getResponseHeader(name)String如果存在,则返回所请求标头的值。否则抛出错误。这只是 xmlHttpRequest 对象的本机方法的包装器。更喜欢它的较短的对应 getHeader。
getAllResponseHeaders()String返回一个字符串,其中包含由换行符分隔的所有标题。否则抛出错误。这只是 xmlHttpRequest 对象的本机方法的包装器。更喜欢它的较短的对应物 getAllHeaders。

例子

以下是显示getAllHeaders()和 getResponseHeader(name)方法用法的示例:

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "https://cdn.bootcdn.net/ajax/libs/prototype/1.7.3/prototype.min.js"></script>
      
      <script>
         function SubmitRequest() {
            new Ajax.Request('/cgi-bin/ajax.cgi', {
               method: 'get',
               onSuccess: successFunc
            });
         }
         function successFunc(response) {
            var content = response.getAllHeaders();
            var container = $(header1);
            container.update(content);
            var content = response.getResponseHeader('Content-Type');
            var container = $(header2);
            container.update(content);
         }
      </script>
   </head>

   <body>
      <p>Click submit button to see the result:</p>
      <br />
 
      <div id = "header1">All Headers</div>
      <div id = "header2">Content Type</div>
      <br />
      <br />
      <input type = "button" value = "Submit" onclick = "SubmitRequest();"/>
   </body>
</html>

返回到:Prototype AJAX 教程

作者:terry,如若转载,请注明出处:https://www.web176.com/prototype_api/8287.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2023年1月25日
下一篇 2023年1月25日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注