Guest User

Untitled

a guest
Dec 12th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.37 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. <cffunction name="optionsMethod" access="remote" output="false" returntype="any" httpmethod="OPTIONS" description="Method to respond to AngularJS trial call">
  68. <cfheader name="Access-Control-Allow-Headers" value="Content-Type,x-requested-with,Authorization,Access-Control-Allow-Origin">
  69. <cfheader name="Access-Control-Allow-Methods" value="GET,OPTIONS">
  70. <cfheader name="Access-Control-Allow-Origin" value="*">
  71. <cfheader name="Access-Control-Max-Age" value="360">
  72. </cffunction>
  73.  
  74. Spark.get("/someRestCallToSpark", (req, res) -> {
  75. res.header("Access-Control-Allow-Origin", "*"); //important, otherwise its not working
  76. return "some text";
  77. });
  78.  
  79. Ext.Ajax.request({
  80. url: "http://localhost:4567/someRestCallToSpark",
  81. useDefaultXhrHeader: false, //important, otherwise its not working
  82. success: function(response, opts) {console.log("success")},
  83. failure: function(response, opts) {console.log("failure")}
  84. });
  85.  
  86. using (DBContext db = new DBContext())
  87. {
  88. return db.Customers.Select(x => new
  89. {
  90. Name = x.Name,
  91. CustomerId = x.CustomerId,
  92. });
  93. }
  94.  
  95. using (DBContext db = new DBContext())
  96. {
  97. return db.Customers.Select(x => new
  98. {
  99. Name = x.Name,
  100. CustomerId = x.CustomerId,
  101. }).ToList();
  102. }
  103.  
  104. @GET
  105. @Path("{id}")
  106. public Response getEventData(@PathParam("id") String id) throws FileNotFoundException {
  107. InputStream inputStream = getClass().getClassLoader().getResourceAsStream("/eventdata/" + id + ".json");
  108. JsonReader jsonReader = Json.createReader(inputStream);
  109. return Response.ok(jsonReader.readObject()).header("Access-Control-Allow-Origin", "*").build();
  110. }
  111.  
  112. var yql_url = 'https://query.yahooapis.com/v1/public/yql';
  113. var url = 'your api url';
  114.  
  115. $.ajax({
  116. 'url': yql_url,
  117. 'data': {
  118. 'q': 'SELECT * FROM json WHERE url="'+url+'"',
  119. 'format': 'json',
  120. 'jsonCompat': 'new',
  121. },
  122. 'dataType': 'jsonp',
  123. 'success': function(response) {
  124. console.log(response);
  125. },
  126. });
  127.  
  128. <IfModule mod_headers.c>
  129. <FilesMatch ".(ttf|ttc|otf|eot|woff|woff2|font.css|css)$">
  130. Header set Access-Control-Allow-Origin "*"
  131. </FilesMatch>
  132. </IfModule>
  133.  
  134. catta('./data/simple.json').then(function (res) {
  135. console.log(res);
  136. });
  137.  
  138. opera --user-data-dir="~/Downloads/opera-session" --disable-web-security
  139.  
  140. <system.webServer>
  141. <httpProtocol>
  142. <customHeaders>
  143. <add name="Access-Control-Allow-Origin" value="*" />
  144. <add name="Access-Control-Allow-Headers" value="Content-Type" />
  145. <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
  146. </customHeaders>
  147. </httpProtocol>
  148. </system.webServer>
  149.  
  150. [EnableCors(origins: "http://clientaccessingapi.com", headers: "*", methods: "*")]
  151.  
  152. after_action :cors_set_access_control_headers
  153.  
  154. def cors_set_access_control_headers
  155. headers['Access-Control-Allow-Origin'] = '*'
  156. headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
  157. headers['Access-Control-Allow-Headers'] = '*'
  158. end
  159.  
  160. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '…' is therefore not allowed access
  161.  
  162. <?php header('Access-Control-Allow-Origin: *'); ?>
  163.  
  164. $.ajax({
  165. url: yourserver.com/controller/proxy.php,
  166. async:false,
  167. type: "POST",
  168. dataType: "json",
  169. data: data,
  170. success: function (result) {
  171. JSON.parse(result);
  172. },
  173. error: function (xhr, ajaxOptions, thrownError) {
  174. console.log(xhr);
  175. }
  176. });
  177.  
  178. if (isset($_POST)) {
  179. $apiKey = $_POST['apiKey'];
  180. $cid = $_POST['cid'];
  181. $minorRev = 99;
  182.  
  183. $url = 'http://api.ean.com/ean-services/rs/hotel/v3/list?' . 'cid='. $cid . '&' . 'minorRev=' . $minorRev . '&' . 'apiKey=' . $apiKey;
  184.  
  185. echo json_encode(file_get_contents($url));
  186. }
  187.  
  188. echo json_encode(file_get_contents($url));
  189.  
  190. Access-Control-Allow-Origin: http://foo.example
  191.  
  192. Access-Control-Allow-Origin: *
  193.  
  194. func main() { //API Server Code
  195. router := mux.NewRouter()
  196. //api route is /people,
  197. //Methods("GET", "OPTIONS") means it support GET, OPTIONS
  198. router.HandleFunc("/people", GetPeople).Methods("GET", "OPTIONS")
  199. log.Fatal(http.ListenAndServe(":9091", router))
  200. }
  201.  
  202. // method of '/people' route
  203. func GetPeople(w http.ResponseWriter, r *http.Request) {
  204.  
  205. //Allow CORS by setting * in sever response
  206. w.Header().Set("Access-Control-Allow-Origin", "*")
  207.  
  208. w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
  209. json.NewEncoder(w).Encode("OKOK")
  210. }
  211.  
  212. function GetPeople() {
  213. try {
  214. var xhttp = new XMLHttpRequest();
  215. xhttp.open("GET", "http://localhost:9091/people", false);
  216. xhttp.setRequestHeader("Content-type", "text/html");
  217. xhttp.send();
  218. var response = JSON.parse(xhttp.response);
  219. alert(xhttp.response);
  220. } catch (error) { alert(error.message);}
  221. }
Add Comment
Please, Sign In to add comment