Guest User

Untitled

a guest
Feb 27th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 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. catta('./data/simple.json').then(function (res) {
  138. console.log(res);
  139. });
  140.  
  141. Access-Control-Allow-Origin: http://foo.example
  142.  
  143. Access-Control-Allow-Origin: *
  144.  
  145. func main() { //API Server Code
  146. router := mux.NewRouter()
  147. //api route is /people,
  148. //Methods("GET", "OPTIONS") means it support GET, OPTIONS
  149. router.HandleFunc("/people", GetPeople).Methods("GET", "OPTIONS")
  150. log.Fatal(http.ListenAndServe(":9091", router))
  151. }
  152.  
  153. // method of '/people' route
  154. func GetPeople(w http.ResponseWriter, r *http.Request) {
  155.  
  156. //Allow CORS by setting * in sever response
  157. w.Header().Set("Access-Control-Allow-Origin", "*")
  158.  
  159. w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
  160. json.NewEncoder(w).Encode("OKOK")
  161. }
  162.  
  163. function GetPeople() {
  164. try {
  165. var xhttp = new XMLHttpRequest();
  166. xhttp.open("GET", "http://localhost:9091/people", false);
  167. xhttp.setRequestHeader("Content-type", "text/html");
  168. xhttp.send();
  169. var response = JSON.parse(xhttp.response);
  170. alert(xhttp.response);
  171. } catch (error) { alert(error.message);}
  172. }
  173.  
  174. window.handleData = function(data) {
  175. console.log(data)
  176. };
  177.  
  178. var script = document.createElement('script');
  179. script.setAttribute('src','https://some.api/without/cors/headers.com&callback=handleData');
  180. document.body.appendChild(script);
  181.  
  182. opera --user-data-dir="~/Downloads/opera-session" --disable-web-security
  183.  
  184. <system.webServer>
  185. <httpProtocol>
  186. <customHeaders>
  187. <add name="Access-Control-Allow-Origin" value="*" />
  188. <add name="Access-Control-Allow-Headers" value="Content-Type" />
  189. <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
  190. </customHeaders>
  191. </httpProtocol>
  192. </system.webServer>
  193.  
  194. [EnableCors(origins: "http://clientaccessingapi.com", headers: "*", methods: "*")]
  195.  
  196. after_action :cors_set_access_control_headers
  197.  
  198. def cors_set_access_control_headers
  199. headers['Access-Control-Allow-Origin'] = '*'
  200. headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
  201. headers['Access-Control-Allow-Headers'] = '*'
  202. end
Add Comment
Please, Sign In to add comment