-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.js
68 lines (57 loc) · 1.43 KB
/
basic.js
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Here you go, pretty simple:
function createXHR()
{
var xhr;
if (window.ActiveXObject)
{
try
{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
alert(e.message);
xhr = null;
}
}
else
{
xhr = new XMLHttpRequest();
}
return xhr;
}
/*
* Documentation is here
* http://msdn.microsoft.com/en-us/library/ms535874%28VS.85%29.aspx
*
* Example:
*/
var xhr = createXHR();
xhr.onreadystatechange = function()
{
if (xhr.readyState === 4)
{
alert(xhr.responseText);
}
}
xhr.open('GET', 'test.txt', true)
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send()
/*
* In order to do cross-domain scripting,
* you'll either have to call out to a local server-side proxy
* (which reads and echo's the remote data),
* or, if your remote service returns JSON, use this method:
*/
var s = document.createElement('script')
s.src = 'remotewebservice.json';
document.body.appendChild(s);
/*
* Since JSON is essentially a JavaScript object or array,
* this is a valid source.
* You theoretically should then be able to call the remote service directly.
* I haven't tested this, but it seems to be an accepted practice:
* Reference:
* Calling Cross Domain Web Services in AJAX
* http://www.simple-talk.com/dotnet/asp.net/calling-cross-domain-web-services-in-ajax/
*/