Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var url = 'http://somewhere.com/';
- var xsrf = {fkey: 'xsrf key'};
- $http({
- method: 'POST',
- url: url,
- data: xsrf
- }).success(function () {});
- $.ajax({
- type: 'POST',
- url: url,
- data: xsrf,
- dataType: 'json',
- success: function() {}
- });
- headers: {'Content-Type': 'application/x-www-form-urlencoded'}
- > $.param({fkey: "key"})
- 'fkey=key'
- $http({
- method: 'POST',
- url: url,
- data: $.param({fkey: "key"}),
- headers: {'Content-Type': 'application/x-www-form-urlencoded'}
- })
- $http({
- method: 'POST',
- url: url,
- headers: {'Content-Type': 'application/x-www-form-urlencoded'},
- transformRequest: function(obj) {
- var str = [];
- for(var p in obj)
- str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
- return str.join("&");
- },
- data: xsrf
- }).success(function () {});
- .config(['$httpProvider', function ($httpProvider) {
- // Intercept POST requests, convert to standard form encoding
- $httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
- $httpProvider.defaults.transformRequest.unshift(function (data, headersGetter) {
- var key, result = [];
- if (typeof data === "string")
- return data;
- for (key in data) {
- if (data.hasOwnProperty(key))
- result.push(encodeURIComponent(key) + "=" + encodeURIComponent(data[key]));
- }
- return result.join("&");
- });
- }]);
- $http.post('http://example.com', $httpParamSerializer(formDataObj)).
- success(function(data){/* response status 200-299 */}).
- error(function(data){/* response status 400-999 */});
- $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
- var req = {
- method: 'POST',
- url: 'http://example.com',
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded'
- },
- data: $httpParamSerializer(formDataObj)
- };
- $http(req);
- $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
- $http.post("/handle/post", {
- foo: "FOO",
- bar: "BAR"
- }).success(function (data, status, headers, config) {
- // TODO
- }).error(function (data, status, headers, config) {
- // TODO
- });
- //handles JSON posted arguments and stuffs them into $_POST
- //angular's $http makes JSON posts (not normal "form encoded")
- $content_type_args = explode(';', $_SERVER['CONTENT_TYPE']); //parse content_type string
- if ($content_type_args[0] == 'application/json')
- $_POST = json_decode(file_get_contents('php://input'),true);
- //now continue to reference $_POST vars as usual
- $http.post(loginUrl, "userName=" + encodeURIComponent(email) +
- "&password=" + encodeURIComponent(password) +
- "&grant_type=password"
- ).success(function (data) {
- //...
- $http({
- method: 'POST',
- url: url-post,
- data: data-post-object-json,
- headers: {'Content-Type': 'application/x-www-form-urlencoded'},
- transformRequest: function(obj) {
- var str = [];
- for (var key in obj) {
- if (obj[key] instanceof Array) {
- for(var idx in obj[key]){
- var subObj = obj[key][idx];
- for(var subKey in subObj){
- str.push(encodeURIComponent(key) + "[" + idx + "][" + encodeURIComponent(subKey) + "]=" + encodeURIComponent(subObj[subKey]));
- }
- }
- }
- else {
- str.push(encodeURIComponent(key) + "=" + encodeURIComponent(obj[key]));
- }
- }
- return str.join("&");
- }
- }).success(function(response) {
- /* Do something */
- });
- $http({
- method : 'POST',
- url : 'url',
- data : $.param(xsrf), // pass in data as strings
- headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload)
- });
- var fd = new FormData();
- fd.append('file', file);
- $http.post(uploadUrl, fd, {
- transformRequest: angular.identity,
- headers: {'Content-Type': undefined}
- })
- .success(function(){
- })
- .error(function(){
- });
- <?php
- namespace AcmeTestMyRequest;
- use SymfonyComponentHttpFoundationRequest;
- use SymfonyComponentHttpFoundationParameterBag;
- class MyRequest extends Request{
- /**
- * Override and extend the createFromGlobals function.
- *
- *
- *
- * @return Request A new request
- *
- * @api
- */
- public static function createFromGlobals()
- {
- // Get what we would get from the parent
- $request = parent::createFromGlobals();
- // Add the handling for 'application/json' content type.
- if(0 === strpos($request->headers->get('CONTENT_TYPE'), 'application/json')){
- // The json is in the content
- $cont = $request->getContent();
- $json = json_decode($cont);
- // ParameterBag must be an Array.
- if(is_object($json)) {
- $json = (array) $json;
- }
- $request->request = new ParameterBag($json);
- }
- return $request;
- }
- }
- // web/app_dev.php
- $kernel = new AppKernel('dev', true);
- // $kernel->loadClassCache();
- $request = ForumBundleRequest::createFromGlobals();
- // use your class instead
- // $request = Request::createFromGlobals();
- $response = $kernel->handle($request);
- $response->send();
- $kernel->terminate($request, $response);
- Request::createFromGlobals()
- Content-Type: application/json
- $request = $this->getRequest();
- if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
- $data = json_decode($request->getContent(), true);
- $request->request->replace(is_array($data) ? $data : array());
- }
- var_dump($request->request->all());
- var res = $resource(serverUrl + 'Token', { }, {
- save: { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
- });
- res.save({ }, $httpParamSerializer({ param1: 'sdsd', param2: 'sdsd' }), function (response) {
- }, function (error) {
- });
- $httpProvider.defaults.transformRequest = function (data) {
- if (data === undefined)
- return data;
- var clonedData = $.extend(true, {}, data);
- for (var property in clonedData)
- if (property.substr(0, 1) == '$')
- delete clonedData[property];
- return $.param(clonedData);
- };
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded'
- }
- services.service('Http', function ($http) {
- var self = this
- this.post = function (url, data) {
- return $http({
- method: 'POST',
- url: url,
- data: $.param(data),
- headers: {'Content-Type': 'application/x-www-form-urlencoded'}
- })
- }
- })
- ctrls.controller('PersonCtrl', function (Http /* our service */) {
- var self = this
- self.user = {name: "Ozgur", eMail: null}
- self.register = function () {
- Http.post('/user/register', self.user).then(function (r) {
- //response
- console.log(r)
- })
- }
- })
- http://www.someexample.com/xsrf/{xsrfKey}
- {
- appointmentId : 23,
- name : 'Joe Citizen',
- xsrf : '...'
- }
- shopLocation=downtown&daysOpen=Monday&daysOpen=Tuesday&daysOpen=Wednesday
- shopLocation=downtwon&daysOpen=Monday,Tuesday,Wednesday
- $http({
- method: 'POST',
- url: serviceUrl + '/ClientUpdate',
- params: { LangUserId: userId, clientJSON: clients[i] },
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement