Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2016
618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 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. // The following property can be used to configure cross-origin resource sharing
  34. // in the HTTP nodes.
  35. // See https://github.com/troygoode/node-cors#configuration-options for
  36. // details on its contents. The following is a basic permissive set of options:
  37. httpNodeCors: {
  38. origin: "*",
  39. methods: "GET,PUT,POST,DELETE"
  40. },
  41.  
  42. app.use(function(req, res, next) {
  43. res.header("Access-Control-Allow-Origin", "*");
  44. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  45. next();
  46. });
  47.  
  48. <cffunction name="optionsMethod" access="remote" output="false" returntype="any" httpmethod="OPTIONS" description="Method to respond to AngularJS trial call">
  49. <cfheader name="Access-Control-Allow-Headers" value="Content-Type,x-requested-with,Authorization,Access-Control-Allow-Origin">
  50. <cfheader name="Access-Control-Allow-Methods" value="GET,OPTIONS">
  51. <cfheader name="Access-Control-Allow-Origin" value="*">
  52. <cfheader name="Access-Control-Max-Age" value="360">
  53. </cffunction>
  54.  
  55. Spark.get("/someRestCallToSpark", (req, res) -> {
  56. res.header("Access-Control-Allow-Origin", "*"); //important, otherwise its not working
  57. return "some text";
  58. });
  59.  
  60. Ext.Ajax.request({
  61. url: "http://localhost:4567/someRestCallToSpark",
  62. useDefaultXhrHeader: false, //important, otherwise its not working
  63. success: function(response, opts) {console.log("success")},
  64. failure: function(response, opts) {console.log("failure")}
  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. });
  74. }
  75.  
  76. using (DBContext db = new DBContext())
  77. {
  78. return db.Customers.Select(x => new
  79. {
  80. Name = x.Name,
  81. CustomerId = x.CustomerId,
  82. }).ToList();
  83. }
  84.  
  85. @GET
  86. @Path("{id}")
  87. public Response getEventData(@PathParam("id") String id) throws FileNotFoundException {
  88. InputStream inputStream = getClass().getClassLoader().getResourceAsStream("/eventdata/" + id + ".json");
  89. JsonReader jsonReader = Json.createReader(inputStream);
  90. return Response.ok(jsonReader.readObject()).header("Access-Control-Allow-Origin", "*").build();
  91. }
  92.  
  93. var yql_url = 'https://query.yahooapis.com/v1/public/yql';
  94. var url = 'your api url';
  95.  
  96. $.ajax({
  97. 'url': yql_url,
  98. 'data': {
  99. 'q': 'SELECT * FROM json WHERE url="'+url+'"',
  100. 'format': 'json',
  101. 'jsonCompat': 'new',
  102. },
  103. 'dataType': 'jsonp',
  104. 'success': function(response) {
  105. console.log(response);
  106. },
  107. });
  108.  
  109. <httpProtocol>
  110. <customHeaders>
  111. <add name="Access-Control-Allow-Origin" value="*" />
  112. </customHeaders>
  113. </httpProtocol>
  114.  
  115. $.ajax({
  116. url: 'http://mysite.microsoft.sample.xyz.com/api/mycall',
  117. headers: {
  118. 'Content-Type': 'application/x-www-form-urlencoded'
  119. },
  120. type: "POST", /* or type:"GET" or type:"PUT" */
  121. dataType: "json",
  122. data: {
  123. },
  124. success: function (result) {
  125. console.log(result);
  126. },
  127. error: function () {
  128. console.log("error");
  129. }
  130. });
  131.  
  132. <filter>
  133. <filter-name>CORS</filter-name>
  134. <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
  135. </filter>
  136. <filter-mapping>
  137. <filter-name>CORS</filter-name>
  138. <url-pattern>/*</url-pattern>
  139. </filter-mapping>
  140.  
  141. /Applications/Google Chrome.app/Contents/MacOS/Google Chrome --allow-file-access-from-files
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement