Guest User

Untitled

a guest
Apr 4th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.82 KB | None | 0 0
  1. $.ajax({
  2. type: "POST",
  3. dataType: 'text',
  4. url: api,
  5. username: 'user',
  6. password: 'pass',
  7. crossDomain : true,
  8. xhrFields: {
  9. withCredentials: true
  10. }
  11. })
  12. .done(function( data ) {
  13. console.log("done");
  14. })
  15. .fail( function(xhr, textStatus, errorThrown) {
  16. alert(xhr.responseText);
  17. alert(textStatus);
  18. });
  19.  
  20. chrome.exe --user-data-dir="C:/Chrome dev session" --disable-web-security
  21.  
  22. $.ajax({
  23. type: "POST",
  24. dataType: 'jsonp',
  25. ...... etc ......
  26.  
  27. <?php header('Access-Control-Allow-Origin: *'); ?>
  28.  
  29. // The following property can be used to configure cross-origin resource sharing
  30. // in the HTTP nodes.
  31. // See https://github.com/troygoode/node-cors#configuration-options for
  32. // details on its contents. The following is a basic permissive set of options:
  33. httpNodeCors: {
  34. origin: "*",
  35. methods: "GET,PUT,POST,DELETE"
  36. },
  37.  
  38. app.use(function(req, res, next) {
  39. res.header("Access-Control-Allow-Origin", "*");
  40. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  41. next();
  42. });
  43.  
  44. <httpProtocol>
  45. <customHeaders>
  46. <add name="Access-Control-Allow-Origin" value="*" />
  47. </customHeaders>
  48. </httpProtocol>
  49.  
  50. $.ajax({
  51. url: 'http://mysite.microsoft.sample.xyz.com/api/mycall',
  52. headers: {
  53. 'Content-Type': 'application/x-www-form-urlencoded'
  54. },
  55. type: "POST", /* or type:"GET" or type:"PUT" */
  56. dataType: "json",
  57. data: {
  58. },
  59. success: function (result) {
  60. console.log(result);
  61. },
  62. error: function () {
  63. console.log("error");
  64. }
  65. });
  66.  
  67. System.Net.WebClient wc = new System.Net.WebClient();
  68. string str = wc.DownloadString("http://mysite.microsoft.sample.xyz.com/api/mycall");
  69.  
  70. <cffunction name="optionsMethod" access="remote" output="false" returntype="any" httpmethod="OPTIONS" description="Method to respond to AngularJS trial call">
  71. <cfheader name="Access-Control-Allow-Headers" value="Content-Type,x-requested-with,Authorization,Access-Control-Allow-Origin">
  72. <cfheader name="Access-Control-Allow-Methods" value="GET,OPTIONS">
  73. <cfheader name="Access-Control-Allow-Origin" value="*">
  74. <cfheader name="Access-Control-Max-Age" value="360">
  75. </cffunction>
  76.  
  77. Spark.get("/someRestCallToSpark", (req, res) -> {
  78. res.header("Access-Control-Allow-Origin", "*"); //important, otherwise its not working
  79. return "some text";
  80. });
  81.  
  82. Ext.Ajax.request({
  83. url: "http://localhost:4567/someRestCallToSpark",
  84. useDefaultXhrHeader: false, //important, otherwise its not working
  85. success: function(response, opts) {console.log("success")},
  86. failure: function(response, opts) {console.log("failure")}
  87. });
  88.  
  89. var yql_url = 'https://query.yahooapis.com/v1/public/yql';
  90. var url = 'your api url';
  91.  
  92. $.ajax({
  93. 'url': yql_url,
  94. 'data': {
  95. 'q': 'SELECT * FROM json WHERE url="'+url+'"',
  96. 'format': 'json',
  97. 'jsonCompat': 'new',
  98. },
  99. 'dataType': 'jsonp',
  100. 'success': function(response) {
  101. console.log(response);
  102. },
  103. });
  104.  
  105. using (DBContext db = new DBContext())
  106. {
  107. return db.Customers.Select(x => new
  108. {
  109. Name = x.Name,
  110. CustomerId = x.CustomerId,
  111. });
  112. }
  113.  
  114. using (DBContext db = new DBContext())
  115. {
  116. return db.Customers.Select(x => new
  117. {
  118. Name = x.Name,
  119. CustomerId = x.CustomerId,
  120. }).ToList();
  121. }
  122.  
  123. @GET
  124. @Path("{id}")
  125. public Response getEventData(@PathParam("id") String id) throws FileNotFoundException {
  126. InputStream inputStream = getClass().getClassLoader().getResourceAsStream("/eventdata/" + id + ".json");
  127. JsonReader jsonReader = Json.createReader(inputStream);
  128. return Response.ok(jsonReader.readObject()).header("Access-Control-Allow-Origin", "*").build();
  129. }
  130.  
  131. <IfModule mod_headers.c>
  132. <FilesMatch ".(ttf|ttc|otf|eot|woff|woff2|font.css|css)$">
  133. Header set Access-Control-Allow-Origin "*"
  134. </FilesMatch>
  135. </IfModule>
  136.  
  137. Access-Control-Allow-Origin: http://foo.example
  138.  
  139. Access-Control-Allow-Origin: *
  140.  
  141. func main() { // API Server Code
  142. router := mux.NewRouter()
  143. // API route is /people,
  144. //Methods("GET", "OPTIONS") means it support GET, OPTIONS
  145. router.HandleFunc("/people", GetPeople).Methods("GET", "OPTIONS")
  146. log.Fatal(http.ListenAndServe(":9091", router))
  147. }
  148.  
  149. // Method of '/people' route
  150. func GetPeople(w http.ResponseWriter, r *http.Request) {
  151.  
  152. // Allow CORS by setting * in sever response
  153. w.Header().Set("Access-Control-Allow-Origin", "*")
  154.  
  155. w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
  156. json.NewEncoder(w).Encode("OKOK")
  157. }
  158.  
  159. function GetPeople() {
  160. try {
  161. var xhttp = new XMLHttpRequest();
  162. xhttp.open("GET", "http://localhost:9091/people", false);
  163. xhttp.setRequestHeader("Content-type", "text/html");
  164. xhttp.send();
  165. var response = JSON.parse(xhttp.response);
  166. alert(xhttp.response);
  167. }
  168. catch (error) {
  169. alert(error.message);
  170. }
  171. }
  172.  
  173. catta('./data/simple.json').then(function (res) {
  174. console.log(res);
  175. });
  176.  
  177. window.handleData = function(data) {
  178. console.log(data)
  179. };
  180.  
  181. var script = document.createElement('script');
  182. script.setAttribute('src','https://some.api/without/cors/headers.com&callback=handleData');
  183. document.body.appendChild(script);
  184.  
  185. after_action :cors_set_access_control_headers
  186.  
  187. def cors_set_access_control_headers
  188. headers['Access-Control-Allow-Origin'] = '*'
  189. headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
  190. headers['Access-Control-Allow-Headers'] = '*'
  191. end
  192.  
  193. opera --user-data-dir="~/Downloads/opera-session" --disable-web-security
  194.  
  195. <system.webServer>
  196. <httpProtocol>
  197. <customHeaders>
  198. <add name="Access-Control-Allow-Origin" value="*" />
  199. <add name="Access-Control-Allow-Headers" value="Content-Type" />
  200. <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
  201. </customHeaders>
  202. </httpProtocol>
  203. </system.webServer>
  204.  
  205. [EnableCors(origins: "http://clientaccessingapi.com", headers: "*", methods: "*")]
  206.  
  207. Failed to load https://www.Domain.in/index.php?route=api/synchronization/checkapikey:
  208. No 'Access-Control-Allow-Origin' header is present on the requested resource.
  209. Origin 'https://sx.xyz.in' is therefore not allowed access.
  210.  
  211. if (($this->request->server['REQUEST_METHOD'] == 'POST') && isset($this->request->server['HTTP_ORIGIN'])) {
  212.  
  213. $this->response->addHeader('Access-Control-Allow-Origin: ' . $this->request->server['HTTP_ORIGIN']);
  214. $this->response->addHeader('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
  215. $this->response->addHeader('Access-Control-Max-Age: 1000');
  216. $this->response->addHeader('Access-Control-Allow-Credentials: true');
  217. $this->response->addHeader('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
  218.  
  219. $headers = getallheaders();
  220. ...
  221. }
Add Comment
Please, Sign In to add comment