Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. app.post('/check', function (req, res) {
  2.  
  3. console.log("Client checking for service...");
  4.  
  5. res.writeHead(200, {
  6. 'Access-Control-Allow-Origin': 'http://localhost',
  7. 'Content-Length': 0,
  8. 'Content-Type': 'text/plain'
  9. });
  10. res.end();
  11. console.log("Client checked in!");
  12.  
  13. });
  14.  
  15. request = $.ajax({
  16. url: "http://127.0.0.1:8080/check",
  17. type: "POST",
  18. crossDomain: true,
  19. data: '',
  20. dataType: "json",
  21.  
  22. error: function () {
  23. alert("Could not connect to the server! Returning to login screen.");
  24. signOut();
  25. },
  26. success: function () {
  27.  
  28. DoStuff();
  29.  
  30. }
  31. });
  32.  
  33. 'Content-Type': 'text/plain'
  34.  
  35. dataType: "json"
  36.  
  37. <p id="results"></p>
  38. <button>localpost</button>
  39.  
  40. $(function() {
  41.  
  42. var localpost = function() {
  43.  
  44. var callback = function(name) {
  45. $("#results").html(name)
  46. };
  47.  
  48. var request = $.ajax({
  49. type:"POST",
  50. dataType: "json",
  51. data: "",
  52. statusCode : {
  53. 200: function(data, textStatus, jqxhr) {
  54. if (textStatus === "success" && jqxhr.responseJSON) {
  55. alert(textStatus+"n Could not connect to the server! Returning to login screen.");
  56. alert(JSON.stringify(jqxhr.responseJSON)+"n"
  57. +String(jqxhr.getResponseHeader("Content-Type")));
  58. // signOut();
  59. };
  60. }
  61. }
  62. });
  63. // `success` callback
  64. request.done(function(data, textStatus, jqxhr) {
  65. if (textStatus === "success") {
  66. // DoStuff()
  67. callback("textStatus: "
  68. +textStatus+"<br />jqxhr status: "
  69. +jqxhr.state()+"<br />content-type: "
  70. +jqxhr.getResponseHeader("Content-Type")+"<br /><br />"
  71. +jqxhr.getResponseHeader("Content-Length")+"<br /><br />"
  72. +jqxhr.getResponseHeader("Access-Control-Allow-Origin")+"<br /><br />"
  73. +JSON.stringify(data))
  74. };
  75. });
  76. // `error` callback
  77. request.fail(function(data, textStatus, jqxhr) {
  78. console.log(textStatus);
  79. })
  80. };
  81.  
  82. $("button").one("click", localpost);
  83. // localpost()
  84.  
  85. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement