Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. <?php
  2.  
  3. require('includes/config.php');
  4.  
  5. $data = array();
  6.  
  7. try {
  8. $stmt = $db->query('SELECT postID, postTitle, postDesc, postDate,tags FROM blog_posts where inMain=inMain ORDER BY postID DESC LIMIT 5');
  9. $error = false;
  10. $message = "ok";
  11. $i=0;
  12. while($row = $stmt->fetch()){
  13.  
  14. $tags = explode(",",$row['tags']);
  15. $finalTags = array();
  16. foreach($tags as $item) {
  17. if($item != '' && $item != ' '){
  18. array_push($finalTags,strtolower(trim($item)));
  19. }
  20. }
  21.  
  22. array_push($data, [
  23. 'id' => $row['postID'],
  24. 'title' => $row['postTitle'],
  25. 'desc' => $row['postDesc'],
  26. 'date' => $row['postDate'],
  27. 'tags' => $finalTags
  28. ]);
  29.  
  30. }
  31. } catch(PDOException $e) {
  32. $message = $e->getMessage();
  33. $error = true;
  34. }
  35. $response = array();
  36. $response["data"] = $data;
  37. $response["error"] = $error;
  38. $response["message"] = $message;
  39.  
  40. header('Access-Control-Allow-Origin: *');
  41. header('Access-Control-Allow-Credentials: true');
  42. header('Access-Control-Allow-Methods: GET,HEAD,OPTIONS,POST,PUT');
  43. header('Access-Control-Allow-Headers: Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers');
  44. header('Content-Type: application/json');
  45.  
  46. echo json_encode($response);
  47. ?>
  48.  
  49. XMLHttpRequest cannot load http://www.example.com/api/v1/getPostsInMain.php. Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response.
  50.  
  51. app.factory("Data", ['$http', 'toaster',
  52. function ($http, toaster) { // This service connects to our REST API
  53.  
  54. var serviceBase = 'http://www.example/v1/';
  55. var conf= { headers: {
  56. 'Access-Control-Allow-Origin':'*',
  57. 'Access-Control-Allow-Methods': 'GET, POST, PATCH, PUT, DELETE, OPTIONS',
  58. 'Access-Control-Allow-Headers': 'Origin, Content-Type, X-Auth-Token',
  59. 'Content-Type': 'application/json'
  60. }
  61. };
  62.  
  63. var obj = {};
  64. obj.toast = function (data) {
  65.  
  66. toaster.pop(data.status, "", data.message, 10000, 'trustedHtml');
  67. }
  68. obj.get = function (q) {
  69. return $http.get(serviceBase + q,conf).then(function (results) {
  70. return results.data;
  71. });
  72. };
  73. obj.post = function (q, object) {
  74. return $http.post(serviceBase + q, object, conf).then(function (results) {
  75. return results.data;
  76. });
  77. };
  78. return obj;
  79. }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement