Skip to content

Instantly share code, notes, and snippets.

@nakajmg
Last active December 14, 2015 11:58
Show Gist options
  • Save nakajmg/5082715 to your computer and use it in GitHub Desktop.
Save nakajmg/5082715 to your computer and use it in GitHub Desktop.
XHRするとSinonのfake serverがレスポンスでJSON返してくれるコード
<html>
<head>
<title>SinonJS Fake XHR Server Test</title>
</head>
<body>
<script src="jquery.1.9.1.js"></script>
<script src="sinon-1.6.0.js"></script>
<script src="test.js"></script>
</body>
</html>
$(function(){
var server = sinon.fakeServer.create();
var responseJson = {
"content" : "sinon fake server success XD"
};
var responseText = JSON.stringify(responseJson);
server.respondWith(
"GET","/sinon",[200,{"Content-Type": "text/plain"},responseText ]
// "GET","/sinon",[404,{"Content-Type": "text/plain"},"404 not found XD" ]
);
function doRequest(){
$.ajax({
url:"/sinon",
type:"GET",
async:"true",
success : function(data){
console.log(data);
},
error : function(data){
console.log(data.status);
console.log(data.statusText);
}
});
}
doRequest();
server.respond();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment