Created
July 2, 2012 09:22
-
-
Save vvakame/3032255 to your computer and use it in GitHub Desktop.
Sinon.JS + jQuery.ajax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script> | |
<script type="text/javascript" src="http://sinonjs.org/releases/sinon-1.4.2.js"></script> | |
<script type="text/javascript" src="test.js"></script> | |
<body> | |
sample | |
</body> | |
<html> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function () { | |
var server = sinon.fakeServer.create(); | |
// GETだとパラメータが後に付くので正規表現使わないと無理 | |
server.respondWith("GET", /http:\/\/api.atnd.org\/events\/.*/, | |
[ | |
200, | |
{"Content-Type":"application/json"}, | |
JSON.stringify([{"id":12, "comment":"Hey there"}]) | |
] | |
); | |
fetch(); | |
server.respond(); | |
server.restore(); | |
}); | |
function fetch() { | |
$.ajax({ | |
url:'http://api.atnd.org/events/', | |
data:{ | |
"keyword_or":"android", | |
"format":"json" | |
}, | |
success:function (data, textStatus, XMLHttpRequest) { | |
console.log(data); | |
alert(data[0].comment); | |
}, | |
error:function (req, status, error) { | |
alert('error: ' + status); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment