forked from apiaryio/language-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript.html
32 lines (32 loc) · 1.26 KB
/
javascript.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<section name="javascript" class="javascript">
<p class="ioDesc">Request</p>
<pre class="incoming"><code class="language-javascript">var xhr = new XMLHttpRequest();
xhr.open("<%= @method.toUpperCase() %>", "<%= @apiUrl %><%= @url %>");
<% for header, value of @headers: %>
xhr.setRequestHeader(<%= @helpers.escape header %>, <%= @helpers.escape value %>);
<% end %>
xhr.onreadystatechange = function () {
if (this.readyState == 4) {
alert('Status: '+this.status+'\nHeaders: '+JSON.stringify(this.getAllResponseHeaders())+'\nBody: '+this.responseText);
}
};
xhr.send(<%= if @body then @helpers.escape(@body) else 'null' %>);</code></pre>
<pre class="jsRunCode" style="display: none">
var xhr = new XMLHttpRequest();
xhr.open('<%= @method.toUpperCase() %>', '<%= @apiUrl %><%= @url %>');
<% for header, value of @headers: %>
xhr.setRequestHeader(<%= @helpers.escape header %>, <%= @helpers.escape value %>);
<% end %>
xhr.onreadystatechange = function () {
if (this.readyState == 4) {
if (typeof cb !== "undefined") {
cb(this);
}
else {
alert('Status: '+this.status+'\nHeaders: '+JSON.stringify(this.getAllResponseHeaders())+'\nBody: '+this.responseText);
}
}
};
xhr.send(<%= if @body then @helpers.escape(@body) else 'null' %>);
</pre>
</section>