-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.htm
68 lines (57 loc) · 1.93 KB
/
demo.htm
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
68
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="js/jquery.ajax-cross-origin.min.js"></script>
</head>
<body>
<select id="service">
<option value="http://ip.jsontest.com/">IP Address</option>
<option value="http://headers.jsontest.com/">HTTP Headers</option>
<option value="http://date.jsontest.com/">Date & Time</option>
<option value="http://echo.jsontest.com/key/value/one/two">Echo JSON</option>
<option value='http://validate.jsontest.com/?json={"key":"value"};'>Validate</option>
<option value="http://code.jsontest.com/">Arbitrary JS Code</option>
<option value="http://cookie.jsontest.com/">Cookie</option>
<option value="http://md5.jsontest.com/?text=[text%20to%20MD5]">MD5</option>
</select><br/>
<input type="text" id="url" style="width: 400px">
<input type="button" id="btn" value="Get JSON">
<br/><br/>
<div id="test" />
<script type="text/javascript">
$(function() {
$( '#service' ).on( 'change', function(){
$( '#url' ).val( $( this ).val() );
});
$( '#url' ).val( $( '#service' ).val() );
$( '#btn' ).click(function(){
var url = $( '#url' ).val()
$.ajax({
crossOrigin: true,
url: url,
contentType: "application/json",
//dataType: "json", //no need. if you use crossOrigin, the dataType will be override with "json"
//charset: 'ISO-8859-1', //use it to define the charset of the target url
context: {},
success: function(data) {
//alert(data);
$( '#test' ).html(data);
}
})
.done(function( data, textStatus, jqXHR ) {
//alert(data);
});
});
/*########################################################
before the use of $.getJSON you need to set {crossOrigin: true} through $.ajaxSetup
$.ajaxSetup({
crossOrigin: true
});
$.getJSON(url, null, function(data) {
$( '#test' ).html(data);
});
########################################################*/
});
</script>
</body>
</html>