●xmlrequest的用法
<html>
<head>
<title>test</title>
<script>
g_xmlHttpReq = new XMLHttpRequest();
function onReplyCallback()
{
if(g_xmlHttpReq.readyState==4 && g_xmlHttpReq.status==200)
{
alert(g_xmlHttpReq.responseText);
}
}
function on_stop_service()
{
g_xmlHttpReq.open("GET","./service/main.php?cmd=1",true);
g_xmlHttpReq.onreadystatechange=onReplyCallback;
g_xmlHttpReq.send(null);
}
</script>
</head>
<body>
<button οnclick="on_stop_service()">关闭服务</button>
</body>
</html>
●Source/WebCore/xml/XMLHttpRequest.idl
enum XMLHttpRequestResponseType {
"",
"arraybuffer",
"blob",
"document",
"json",
"text"
};
[
GlobalContext=DOMWindow&WorkerGlobalScope,//构造函数被添加到window同时WorkerGlobalScope之上
ActiveDOMObject,//指定DOM对象保持激活状态(只要有事件状态下),webcore实现需要继承 public ActiveDOMObject
Constructor,//js调用形式为new Object()
ConstructorCallWith=ScriptExecutionContext,//参数首个加入ScriptExecutionContext
JSCustomMarkFunction,//自定义JSCbinging的代码( JSXXX::visitChildren() )
EventTarget,//对应需要重载 addEventListener() removeEventListener() 具备事件管理的能力
JSNoStaticTables//默认情况为单一的property table,如果要求每个用户维护不同的表,则需要设定该flag
] interface XMLHttpRequest {
// event handler attributes
attribute EventListener onabort;
attribute EventListener onerror;
attribute EventListener onload;
attribute EventListener onloadend;
attribute EventListener onloadstart;
attribute EventListener onprogress;
[Conditional=XHR_TIMEOUT] attribute EventListener ontimeout;
// event handler attributes
//用法: g_xmlHttpReq.onreadystatechange=onReplyCallback;
attribute EventListener onreadystatechange;
// state
const unsigned short UNSENT = 0;
const unsigned short OPENED = 1;
const unsigned short HEADERS_RECEIVED = 2;
const unsigned short LOADING = 3;
const unsigned short DONE = 4;
[Conditional=XHR_TIMEOUT, SetterRaisesException] attribute unsigned long timeout;
//if(g_xmlHttpReq.readyState==4 && g_xmlHttpReq.status==200)
readonly attribute unsigned short readyState;
[SetterRaisesException] attribute boolean withCredentials;
// optional修饰表示可传,可不传
// g_xmlHttpReq.open("GET","./service/main.php?cmd=1",true);
[Custom, RaisesException] void open(DOMString method, DOMString url, optional boolean async, optional DOMString user, optional DOMString password);
// xmlHttpRequest可以精确到任何可能的HTTP的字段的模拟
[RaisesException] void setRequestHeader(DOMString header, DOMString value);
//在webcore实现的参数中末尾追加(ExceptionCode& ec)
//JSValue JSXMLHttpRequest::send(ExecState* exec)
[Custom, RaisesException] void send();
void abort();
readonly attribute XMLHttpRequestUpload upload;
// response
[TreatReturnedNullStringAs=Undefined, RaisesException] DOMString getAllResponseHeaders();
[TreatReturnedNullStringAs=Null, RaisesException] DOMString getResponseHeader(DOMString header);
// JSValue JSXMLHttpRequest::responseText(ExecState* exec) const
[GetterRaisesException, CustomGetter] readonly attribute DOMString responseText; // The custom getter implements TreatReturnedNullStringAs=Null
[GetterRaisesException] readonly attribute Document responseXML;
[SetterRaisesException] attribute XMLHttpRequestResponseType responseType;
// JSValue JSXMLHttpRequest::send(ExecState* exec)
[GetterRaisesException, CustomGetter] readonly attribute Object response;
[GetterRaisesException] readonly attribute unsigned short status;
[GetterRaisesException] readonly attribute DOMString statusText;
// Extension
void overrideMimeType(DOMString override);
// EventTarget interface
void addEventListener(DOMString type,
EventListener listener,
optional boolean useCapture);
void removeEventListener(DOMString type,
EventListener listener,
optional boolean useCapture);
[RaisesException] boolean dispatchEvent(Event evt);
};
● Source/WebCore/bindings/js/JSXMLHttpRequestCustom.cpp
## 局部函数进行自定义实现
● Source/WebCore/xml/XMLHttpRequest.cpp
●创建对象
PassRefPtr<XMLHttpRequest> XMLHttpRequest::create(ScriptExecutionContext* context)
{
}
## 构造对象
XMLHttpRequest::XMLHttpRequest(ScriptExecutionContext* context)
{
}
## 访问Document
Document* XMLHttpRequest::document() const
{
return static_cast<Document*>(scriptExecutionContext());
}
## 获取安全策略
SecurityOrigin* XMLHttpRequest::securityOrigin() const
>>return scriptExecutionContext()->securityOrigin();
## 访问XHR的运行状态
XMLHttpRequest::State XMLHttpRequest::readyState() const
>>return m_state;
String XMLHttpRequest::responseText(ExceptionCode& ec)
>>return m_responseBuilder.toStringPreserveCapacity();
Document* XMLHttpRequest::responseXML(ExceptionCode& ec)
>> m_responseDocument = HTMLDocument::create(0, m_url);
>> m_responseDocument = Document::create(0, m_url);
>> m_responseDocument->setContent(m_responseBuilder.toStringPreserveCapacity());
>> return m_responseDocument.get();
ArrayBuffer* XMLHttpRequest::responseArrayBuffer(ExceptionCode& ec)
## 上传对象
XMLHttpRequestUpload* XMLHttpRequest::upload()
>>m_upload = XMLHttpRequestUpload::create(this);
## 改变状态
void XMLHttpRequest::changeState(State newState)
>>if (m_state != newState)
>>callReadyStateChangeListener();
void XMLHttpRequest::send(Document* document, ExceptionCode& ec)
>>setRequestHeaderInternal("Content-Type", "application/xml");
>>createRequest(ec);
void XMLHttpRequest::send(ArrayBuffer* body, ExceptionCode& ec)
>>sendBytesData(body->data(), body->byteLength(), ec);
>>createRequest(ec);
void XMLHttpRequest::send(ArrayBufferView* body, ExceptionCode& ec)
>>sendBytesData(body->baseAddress(), body->byteLength(), ec);
>>>>createRequest(ec);
void XMLHttpRequest::createRequest(ExceptionCode& ec)
>>通知
>>ResourceRequest request(m_url);
>>request.setHTTPMethod(m_method);
>>ThreadableLoader::loadResourceSynchronously(scriptExecutionContext(), request, *this, options);
>>m_loader = ThreadableLoader::create(scriptExecutionContext(), this, request, options);
WebKit之XMLHttpRequest简单分析
最新推荐文章于 2020-04-21 01:06:56 发布