Guest User

Untitled

a guest
Jul 18th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.42 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. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '…' is therefore not allowed access
  138.  
  139. <?php header('Access-Control-Allow-Origin: *'); ?>
  140.  
  141. $.ajax({
  142. url: yourserver.com/controller/proxy.php,
  143. async: false,
  144. type: "POST",
  145. dataType: "json",
  146. data: data,
  147. success: function (result) {
  148. JSON.parse(result);
  149. },
  150. error: function (xhr, ajaxOptions, thrownError) {
  151. console.log(xhr);
  152. }
  153. });
  154.  
  155. if (isset($_POST)) {
  156. $apiKey = $_POST['apiKey'];
  157. $cid = $_POST['cid'];
  158. $minorRev = 99;
  159.  
  160. $url = 'http://api.ean.com/ean-services/rs/hotel/v3/list?' . 'cid='. $cid . '&' . 'minorRev=' . $minorRev . '&' . 'apiKey=' . $apiKey;
  161.  
  162. echo json_encode(file_get_contents($url));
  163. }
  164.  
  165. echo json_encode(file_get_contents($url));
  166.  
  167. Access-Control-Allow-Origin: http://foo.example
  168.  
  169. Access-Control-Allow-Origin: *
  170.  
  171. func main() { // API Server Code
  172. router := mux.NewRouter()
  173. // API route is /people,
  174. //Methods("GET", "OPTIONS") means it support GET, OPTIONS
  175. router.HandleFunc("/people", GetPeople).Methods("GET", "OPTIONS")
  176. log.Fatal(http.ListenAndServe(":9091", router))
  177. }
  178.  
  179. // Method of '/people' route
  180. func GetPeople(w http.ResponseWriter, r *http.Request) {
  181.  
  182. // Allow CORS by setting * in sever response
  183. w.Header().Set("Access-Control-Allow-Origin", "*")
  184.  
  185. w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
  186. json.NewEncoder(w).Encode("OKOK")
  187. }
  188.  
  189. function GetPeople() {
  190. try {
  191. var xhttp = new XMLHttpRequest();
  192. xhttp.open("GET", "http://localhost:9091/people", false);
  193. xhttp.setRequestHeader("Content-type", "text/html");
  194. xhttp.send();
  195. var response = JSON.parse(xhttp.response);
  196. alert(xhttp.response);
  197. }
  198. catch (error) {
  199. alert(error.message);
  200. }
  201. }
  202.  
  203. catta('./data/simple.json').then(function (res) {
  204. console.log(res);
  205. });
  206.  
  207. window.handleData = function(data) {
  208. console.log(data)
  209. };
  210.  
  211. var script = document.createElement('script');
  212. script.setAttribute('src','https://some.api/without/cors/headers.com&callback=handleData');
  213. document.body.appendChild(script);
  214.  
  215. <system.webServer>
  216. <httpProtocol>
  217. <customHeaders>
  218. <add name="Access-Control-Allow-Origin" value="*" />
  219. <add name="Access-Control-Allow-Headers" value="Content-Type" />
  220. <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
  221. </customHeaders>
  222. </httpProtocol>
  223. </system.webServer>
  224.  
  225. after_action :cors_set_access_control_headers
  226.  
  227. def cors_set_access_control_headers
  228. headers['Access-Control-Allow-Origin'] = '*'
  229. headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
  230. headers['Access-Control-Allow-Headers'] = '*'
  231. end
  232.  
  233. var rp = require('request-promise');
  234. var express = require('express'),
  235. app = express(),
  236. port = process.env.PORT || 3000;
  237. var options = {
  238. method: 'POST',
  239. uri: 'http://api.posttestserver.com/post',
  240. body: {
  241. some: 'payload'
  242. },
  243. json: true // Automatically stringifies the body to JSON
  244. };
  245. app.get('/', function (req, res) {
  246. rp(options)
  247. .then(function (parsedBody) {
  248. res.send(parsedBody)
  249. })
  250. .catch(function (err) {
  251. res.send(err)
  252. });
  253. });
  254. app.listen(port);
  255.  
  256. axios.get("http://localhost:3000/").then((res)=>{
  257. console.log('================res====================');
  258. console.log(res);
  259. console.log('====================================');
  260. })
  261.  
  262. opera --user-data-dir="~/Downloads/opera-session" --disable-web-security
Add Comment
Please, Sign In to add comment