Advertisement
Guest User

Untitled

a guest
Oct 12th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 KB | None | 0 0
  1. var url = 'http://somewhere.com/';
  2. var xsrf = {fkey: 'xsrf key'};
  3.  
  4. $http({
  5. method: 'POST',
  6. url: url,
  7. data: xsrf
  8. }).success(function () {});
  9.  
  10. $.ajax({
  11. type: 'POST',
  12. url: url,
  13. data: xsrf,
  14. dataType: 'json',
  15. success: function() {}
  16. });
  17.  
  18. headers: {'Content-Type': 'application/x-www-form-urlencoded'}
  19.  
  20. > $.param({fkey: "key"})
  21. 'fkey=key'
  22.  
  23. $http({
  24. method: 'POST',
  25. url: url,
  26. data: $.param({fkey: "key"}),
  27. headers: {'Content-Type': 'application/x-www-form-urlencoded'}
  28. })
  29.  
  30. $http({
  31. method: 'POST',
  32. url: url,
  33. headers: {'Content-Type': 'application/x-www-form-urlencoded'},
  34. transformRequest: function(obj) {
  35. var str = [];
  36. for(var p in obj)
  37. str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
  38. return str.join("&");
  39. },
  40. data: xsrf
  41. }).success(function () {});
  42.  
  43. .config(['$httpProvider', function ($httpProvider) {
  44. // Intercept POST requests, convert to standard form encoding
  45. $httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
  46. $httpProvider.defaults.transformRequest.unshift(function (data, headersGetter) {
  47. var key, result = [];
  48.  
  49. if (typeof data === "string")
  50. return data;
  51.  
  52. for (key in data) {
  53. if (data.hasOwnProperty(key))
  54. result.push(encodeURIComponent(key) + "=" + encodeURIComponent(data[key]));
  55. }
  56. return result.join("&");
  57. });
  58. }]);
  59.  
  60. $http.post('http://example.com', $httpParamSerializer(formDataObj)).
  61. success(function(data){/* response status 200-299 */}).
  62. error(function(data){/* response status 400-999 */});
  63.  
  64. $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
  65.  
  66. var req = {
  67. method: 'POST',
  68. url: 'http://example.com',
  69. headers: {
  70. 'Content-Type': 'application/x-www-form-urlencoded'
  71. },
  72. data: $httpParamSerializer(formDataObj)
  73. };
  74.  
  75. $http(req);
  76.  
  77. $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
  78.  
  79. $http.post("/handle/post", {
  80. foo: "FOO",
  81. bar: "BAR"
  82. }).success(function (data, status, headers, config) {
  83. // TODO
  84. }).error(function (data, status, headers, config) {
  85. // TODO
  86. });
  87.  
  88. //handles JSON posted arguments and stuffs them into $_POST
  89. //angular's $http makes JSON posts (not normal "form encoded")
  90. $content_type_args = explode(';', $_SERVER['CONTENT_TYPE']); //parse content_type string
  91. if ($content_type_args[0] == 'application/json')
  92. $_POST = json_decode(file_get_contents('php://input'),true);
  93.  
  94. //now continue to reference $_POST vars as usual
  95.  
  96. $http.post(loginUrl, "userName=" + encodeURIComponent(email) +
  97. "&password=" + encodeURIComponent(password) +
  98. "&grant_type=password"
  99. ).success(function (data) {
  100. //...
  101.  
  102. $http({
  103. method: 'POST',
  104. url: url-post,
  105. data: data-post-object-json,
  106. headers: {'Content-Type': 'application/x-www-form-urlencoded'},
  107. transformRequest: function(obj) {
  108. var str = [];
  109. for (var key in obj) {
  110. if (obj[key] instanceof Array) {
  111. for(var idx in obj[key]){
  112. var subObj = obj[key][idx];
  113. for(var subKey in subObj){
  114. str.push(encodeURIComponent(key) + "[" + idx + "][" + encodeURIComponent(subKey) + "]=" + encodeURIComponent(subObj[subKey]));
  115. }
  116. }
  117. }
  118. else {
  119. str.push(encodeURIComponent(key) + "=" + encodeURIComponent(obj[key]));
  120. }
  121. }
  122. return str.join("&");
  123. }
  124. }).success(function(response) {
  125. /* Do something */
  126. });
  127.  
  128. $http({
  129. method : 'POST',
  130. url : 'url',
  131. data : $.param(xsrf), // pass in data as strings
  132. headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload)
  133. });
  134.  
  135. var fd = new FormData();
  136. fd.append('file', file);
  137. $http.post(uploadUrl, fd, {
  138. transformRequest: angular.identity,
  139. headers: {'Content-Type': undefined}
  140. })
  141. .success(function(){
  142. })
  143. .error(function(){
  144. });
  145.  
  146. <?php
  147.  
  148. namespace AcmeTestMyRequest;
  149.  
  150. use SymfonyComponentHttpFoundationRequest;
  151. use SymfonyComponentHttpFoundationParameterBag;
  152.  
  153. class MyRequest extends Request{
  154.  
  155.  
  156. /**
  157. * Override and extend the createFromGlobals function.
  158. *
  159. *
  160. *
  161. * @return Request A new request
  162. *
  163. * @api
  164. */
  165. public static function createFromGlobals()
  166. {
  167. // Get what we would get from the parent
  168. $request = parent::createFromGlobals();
  169.  
  170. // Add the handling for 'application/json' content type.
  171. if(0 === strpos($request->headers->get('CONTENT_TYPE'), 'application/json')){
  172.  
  173. // The json is in the content
  174. $cont = $request->getContent();
  175.  
  176. $json = json_decode($cont);
  177.  
  178. // ParameterBag must be an Array.
  179. if(is_object($json)) {
  180. $json = (array) $json;
  181. }
  182. $request->request = new ParameterBag($json);
  183.  
  184. }
  185.  
  186. return $request;
  187.  
  188. }
  189.  
  190. }
  191.  
  192. // web/app_dev.php
  193.  
  194. $kernel = new AppKernel('dev', true);
  195. // $kernel->loadClassCache();
  196. $request = ForumBundleRequest::createFromGlobals();
  197.  
  198. // use your class instead
  199. // $request = Request::createFromGlobals();
  200. $response = $kernel->handle($request);
  201. $response->send();
  202. $kernel->terminate($request, $response);
  203.  
  204. Request::createFromGlobals()
  205.  
  206. Content-Type: application/json
  207.  
  208. $request = $this->getRequest();
  209. if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
  210. $data = json_decode($request->getContent(), true);
  211. $request->request->replace(is_array($data) ? $data : array());
  212. }
  213. var_dump($request->request->all());
  214.  
  215. var res = $resource(serverUrl + 'Token', { }, {
  216. save: { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
  217. });
  218.  
  219. res.save({ }, $httpParamSerializer({ param1: 'sdsd', param2: 'sdsd' }), function (response) {
  220.  
  221. }, function (error) {
  222.  
  223. });
  224.  
  225. $httpProvider.defaults.transformRequest = function (data) {
  226. if (data === undefined)
  227. return data;
  228. var clonedData = $.extend(true, {}, data);
  229. for (var property in clonedData)
  230. if (property.substr(0, 1) == '$')
  231. delete clonedData[property];
  232.  
  233. return $.param(clonedData);
  234. };
  235.  
  236. headers: {
  237. 'Content-Type': 'application/x-www-form-urlencoded'
  238. }
  239.  
  240. services.service('Http', function ($http) {
  241.  
  242. var self = this
  243.  
  244. this.post = function (url, data) {
  245. return $http({
  246. method: 'POST',
  247. url: url,
  248. data: $.param(data),
  249. headers: {'Content-Type': 'application/x-www-form-urlencoded'}
  250. })
  251. }
  252.  
  253. })
  254.  
  255. ctrls.controller('PersonCtrl', function (Http /* our service */) {
  256. var self = this
  257. self.user = {name: "Ozgur", eMail: null}
  258.  
  259. self.register = function () {
  260. Http.post('/user/register', self.user).then(function (r) {
  261. //response
  262. console.log(r)
  263. })
  264. }
  265.  
  266. })
  267.  
  268. http://www.someexample.com/xsrf/{xsrfKey}
  269.  
  270. {
  271. appointmentId : 23,
  272. name : 'Joe Citizen',
  273. xsrf : '...'
  274. }
  275.  
  276. shopLocation=downtown&daysOpen=Monday&daysOpen=Tuesday&daysOpen=Wednesday
  277.  
  278. shopLocation=downtwon&daysOpen=Monday,Tuesday,Wednesday
  279.  
  280. $http({
  281. method: 'POST',
  282. url: serviceUrl + '/ClientUpdate',
  283. params: { LangUserId: userId, clientJSON: clients[i] },
  284. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement