зеркало из https://github.com/mozilla/pjs.git
Bug 98928, send() can take any type of parameter, i.e. it can now take JavaScript string. r=jst, sr=jband.
This commit is contained in:
Родитель
a656590a72
Коммит
8b74570d44
|
@ -41,6 +41,7 @@
|
|||
interface nsIDOMDocument;
|
||||
interface nsIDOMEventListener;
|
||||
interface nsIChannel;
|
||||
interface nsIVariant;
|
||||
|
||||
/**
|
||||
* Mozilla's XMLHttpRequest is modelled after Microsoft's IXMLHttpRequest
|
||||
|
@ -201,7 +202,7 @@ interface nsIXMLHttpRequest : nsISupports {
|
|||
* to contain the ContentType: and ContentLength: headers
|
||||
* and trailing carriage-return/line-feed pairs.
|
||||
*/
|
||||
void send(in nsISupports body);
|
||||
void send(in nsIVariant body);
|
||||
|
||||
/**
|
||||
* Sets a HTTP request header for HTTP requests. You must call open
|
||||
|
|
|
@ -77,6 +77,7 @@
|
|||
#endif
|
||||
#include "nsIDOMClassInfo.h"
|
||||
#include "nsIDOMElement.h"
|
||||
#include "nsIVariant.h"
|
||||
#include "nsIParser.h"
|
||||
#include "nsLoadListenerProxy.h"
|
||||
|
||||
|
@ -945,9 +946,9 @@ nsXMLHttpRequest::RequestCompleted()
|
|||
return rv;
|
||||
}
|
||||
|
||||
/* void send (in nsISupports body); */
|
||||
/* void send (in nsIVariant aBody); */
|
||||
NS_IMETHODIMP
|
||||
nsXMLHttpRequest::Send(nsISupports *body)
|
||||
nsXMLHttpRequest::Send(nsIVariant *aBody)
|
||||
{
|
||||
nsresult rv;
|
||||
|
||||
|
@ -969,42 +970,61 @@ nsXMLHttpRequest::Send(nsISupports *body)
|
|||
httpChannel->GetRequestMethod(getter_Copies(method)); // If GET, method name will be uppercase
|
||||
}
|
||||
|
||||
if (body && httpChannel && nsCRT::strcmp("GET",method.get()) != 0) {
|
||||
if (aBody && httpChannel && nsCRT::strcmp("GET", method.get()) != 0) {
|
||||
nsXPIDLString serial;
|
||||
nsCOMPtr<nsIInputStream> postDataStream;
|
||||
|
||||
nsCOMPtr<nsIDOMDocument> doc(do_QueryInterface(body));
|
||||
if (doc) {
|
||||
// Get an XML serializer
|
||||
nsCOMPtr<nsIDOMSerializer> serializer(do_CreateInstance(NS_XMLSERIALIZER_CONTRACTID, &rv));
|
||||
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
|
||||
|
||||
// Serialize the current document to string
|
||||
nsXPIDLString serial;
|
||||
rv = serializer->SerializeToString(doc, getter_Copies(serial));
|
||||
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
|
||||
PRUint16 dataType;
|
||||
rv = aBody->GetDataType(&dataType);
|
||||
if (NS_FAILED(rv))
|
||||
return NS_ERROR_FAILURE;
|
||||
|
||||
if (dataType == nsIDataType::VTYPE_INTERFACE ||
|
||||
dataType == nsIDataType::VTYPE_INTERFACE_IS) {
|
||||
nsCOMPtr<nsISupports> supports;
|
||||
nsID *iid;
|
||||
rv = aBody->GetAsInterface(&iid, getter_AddRefs(supports));
|
||||
if (NS_FAILED(rv))
|
||||
return NS_ERROR_FAILURE;
|
||||
if (iid)
|
||||
nsMemory::Free(iid);
|
||||
|
||||
// document?
|
||||
nsCOMPtr<nsIDOMDocument> doc(do_QueryInterface(supports));
|
||||
if (doc) {
|
||||
nsCOMPtr<nsIDOMSerializer> serializer(do_CreateInstance(NS_XMLSERIALIZER_CONTRACTID, &rv));
|
||||
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
|
||||
|
||||
rv = serializer->SerializeToString(doc, getter_Copies(serial));
|
||||
if (NS_FAILED(rv))
|
||||
return NS_ERROR_FAILURE;
|
||||
} else {
|
||||
// nsISupportsWString?
|
||||
nsCOMPtr<nsISupportsWString> wstr(do_QueryInterface(supports));
|
||||
if (wstr) {
|
||||
wstr->GetData(getter_Copies(serial));
|
||||
} else {
|
||||
// stream?
|
||||
nsCOMPtr<nsIInputStream> stream(do_QueryInterface(supports));
|
||||
if (stream) {
|
||||
postDataStream = stream;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// try variant string
|
||||
rv = aBody->GetAsWString(getter_Copies(serial));
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
}
|
||||
|
||||
if (serial) {
|
||||
// Convert to a byte stream
|
||||
rv = GetStreamForWString(serial.get(),
|
||||
nsCRT::strlen(serial.get()),
|
||||
getter_AddRefs(postDataStream));
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
}
|
||||
else {
|
||||
nsCOMPtr<nsIInputStream> stream(do_QueryInterface(body));
|
||||
if (stream) {
|
||||
postDataStream = stream;
|
||||
}
|
||||
else {
|
||||
nsCOMPtr<nsISupportsWString> wstr(do_QueryInterface(body));
|
||||
if (wstr) {
|
||||
nsXPIDLString holder;
|
||||
wstr->GetData(getter_Copies(holder));
|
||||
rv = GetStreamForWString(holder.get(),
|
||||
nsCRT::strlen(holder.get()),
|
||||
getter_AddRefs(postDataStream));
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
}
|
||||
}
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
}
|
||||
|
||||
if (postDataStream) {
|
||||
|
|
|
@ -0,0 +1,75 @@
|
|||
<html>
|
||||
<head><title>POST test</title>
|
||||
<style type="text/css">
|
||||
.box {
|
||||
display: box;
|
||||
border: 1px solid black;
|
||||
margin-bottom: 0.5em;
|
||||
}
|
||||
.boxheader {
|
||||
font-weight: bold;
|
||||
color: maroon;
|
||||
}
|
||||
pre {
|
||||
margin-left: 2em;
|
||||
}
|
||||
</style>
|
||||
<script type="text/javascript">
|
||||
var p = new XMLHttpRequest();
|
||||
|
||||
function myfunc(e)
|
||||
{
|
||||
document.getElementById("id1").firstChild.nodeValue = p.responseText;
|
||||
if (p.responseXML) {
|
||||
var s = new XMLSerializer();
|
||||
var d = p.responseXML;
|
||||
var str = s.serializeToString(d);
|
||||
document.getElementById("id2").firstChild.nodeValue = str;
|
||||
}
|
||||
document.getElementById("id3").firstChild.nodeValue = p.getAllResponseHeaders();
|
||||
document.getElementById("id4").firstChild.nodeValue = p.status;
|
||||
document.getElementById("id5").firstChild.nodeValue = p.statusText;
|
||||
document.getElementById("id6").firstChild.nodeValue = p.readyState;
|
||||
var eventProperties = "";
|
||||
for (prop in e) {
|
||||
eventProperties += prop + " : '" + e[prop] + "'\n";
|
||||
}
|
||||
document.getElementById("id7").firstChild.nodeValue =
|
||||
"Event object: " + e + "\n" +
|
||||
"Event properties:\n" + eventProperties;
|
||||
}
|
||||
|
||||
p.onload = myfunc;
|
||||
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
|
||||
p.open("POST", "http://green/cgi-bin/echo_xml.cgi");
|
||||
|
||||
p.send("Heikki's data");
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>POST test</h1>
|
||||
|
||||
<div class="box"><span class="boxheader">responseText</span>
|
||||
<pre id="id1">@@No result@@</pre>
|
||||
</div>
|
||||
<div class="box"><span class="boxheader">responseXML serialized</span>
|
||||
<pre id="id2">@@No result@@</pre>
|
||||
</div>
|
||||
<div class="box"><span class="boxheader">getAllResponseHeaders()</span>
|
||||
<pre id="id3">@@No result@@</pre>
|
||||
</div>
|
||||
<div class="box"><span class="boxheader">status</span>
|
||||
<pre id="id4">@@No result@@</pre>
|
||||
</div>
|
||||
<div class="box"><span class="boxheader">statusText</span>
|
||||
<pre id="id5">@@No result@@</pre>
|
||||
</div>
|
||||
<div class="box"><span class="boxheader">readyState</span>
|
||||
<pre id="id6">@@No result@@</pre>
|
||||
</div>
|
||||
<div class="box"><span class="boxheader">Event information</span>
|
||||
<pre id="id7">@@No result@@</pre>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
Загрузка…
Ссылка в новой задаче