Advertisement
Guest User

Untitled

a guest
Nov 12th, 2016
4,618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. XMLHttpRequest cannot load http://myApiUrl/login. No
  2. 'Access-Control-Allow-Origin' header is present on the requested
  3. resource. Origin 'null' is therefore not allowed access.
  4.  
  5. $.ajax({
  6. type: "POST",
  7. dataType: 'text',
  8. url: api,
  9. username: 'user',
  10. password: 'pass',
  11. crossDomain : true,
  12. xhrFields: {
  13. withCredentials: true
  14. }
  15. })
  16. .done(function( data ) {
  17. console.log("done");
  18. })
  19. .fail( function(xhr, textStatus, errorThrown) {
  20. alert(xhr.responseText);
  21. alert(textStatus);
  22. });
  23.  
  24. chrome.exe --user-data-dir="C:/Chrome dev session" --disable-web-security
  25.  
  26. $.ajax({
  27. type: "POST",
  28. dataType: 'jsonp',
  29. ...... etc ....
  30.  
  31. <?php header('Access-Control-Allow-Origin: *'); ?>
  32.  
  33. app.use(function(req, res, next) {
  34. res.header("Access-Control-Allow-Origin", "*");
  35. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  36. next();
  37. });
  38.  
  39. <cffunction name="optionsMethod" access="remote" output="false" returntype="any" httpmethod="OPTIONS" description="Method to respond to AngularJS trial call">
  40. <cfheader name="Access-Control-Allow-Headers" value="Content-Type,x-requested-with,Authorization,Access-Control-Allow-Origin">
  41. <cfheader name="Access-Control-Allow-Methods" value="GET,OPTIONS">
  42. <cfheader name="Access-Control-Allow-Origin" value="*">
  43. <cfheader name="Access-Control-Max-Age" value="360">
  44. </cffunction>
  45.  
  46. Spark.get("/someRestCallToSpark", (req, res) -> {
  47. res.header("Access-Control-Allow-Origin", "*"); //important, otherwise its not working
  48. return "some text";
  49. });
  50.  
  51. Ext.Ajax.request({
  52. url: "http://localhost:4567/someRestCallToSpark",
  53. useDefaultXhrHeader: false, //important, otherwise its not working
  54. success: function(response, opts) {console.log("success")},
  55. failure: function(response, opts) {console.log("failure")}
  56. });
  57.  
  58. using (DBContext db = new DBContext())
  59. {
  60. return db.Customers.Select(x => new
  61. {
  62. Name = x.Name,
  63. CustomerId = x.CustomerId,
  64. });
  65. }
  66.  
  67. using (DBContext db = new DBContext())
  68. {
  69. return db.Customers.Select(x => new
  70. {
  71. Name = x.Name,
  72. CustomerId = x.CustomerId,
  73. }).ToList();
  74. }
  75.  
  76. @GET
  77. @Path("{id}")
  78. public Response getEventData(@PathParam("id") String id) throws FileNotFoundException {
  79. InputStream inputStream = getClass().getClassLoader().getResourceAsStream("/eventdata/" + id + ".json");
  80. JsonReader jsonReader = Json.createReader(inputStream);
  81. return Response.ok(jsonReader.readObject()).header("Access-Control-Allow-Origin", "*").build();
  82. }
  83.  
  84. $.ajax({
  85. type: "POST",
  86. dataType: 'text',
  87. url: https://crossorigin.me/api,
  88. username: 'user',
  89. password: 'pass',
  90. crossDomain : true,
  91. xhrFields: {
  92. withCredentials: true
  93. }
  94. })
  95. .done(function( data ) {
  96. console.log("done");
  97. })
  98. .fail( function(xhr, textStatus, errorThrown) {
  99. alert(xhr.responseText);
  100. alert(textStatus);
  101. });
  102.  
  103. <filter>
  104. <filter-name>CORS</filter-name>
  105. <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
  106. </filter>
  107. <filter-mapping>
  108. <filter-name>CORS</filter-name>
  109. <url-pattern>/*</url-pattern>
  110. </filter-mapping>
  111.  
  112. /Applications/Google Chrome.app/Contents/MacOS/Google Chrome --allow-file-access-from-files
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement