Last active
December 14, 2015 11:58
-
-
Save nakajmg/5082715 to your computer and use it in GitHub Desktop.
XHRするとSinonのfake serverがレスポンスでJSON返してくれるコード
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> | |
<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> |
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(); | |
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