Friday, 15 June 2012

javascript - CORS working fine in jquery but not in angularjs -



javascript - CORS working fine in jquery but not in angularjs -

my sever side php/mysql.

i making ajax phone call in webservice of domain(which have access command enabled *)

var posturl = "http://logical-brains.com/elance_clone/test_login.php"; var postdata = {username : "tanmoy" , password : "123456"};

i tried simple jquery :

$.ajax({ type: "post", url: posturl, data: postdata, datatype: "json", crossdomain: true, success: function(data){ console.log(json.stringify(data)); } });

it worked fine , got status code:200 ok expected result.

but when trying angularjs , getting below error :

xmlhttprequest cannot load http://logical-brains.com/elance_clone/test_login.php. request header field content-type not allowed access-control-allow-headers.

angularjs code :

var myapp = angular.module('myapp', []); myapp.config(['$httpprovider', function($httpprovider) { $httpprovider.defaults.usexdomain = true; delete $httpprovider.defaults.headers.common['x-requested-with']; }]); myapp.controller('mainctrl', function($scope, $http) { var posturl = "http://logical-brains.com/elance_clone/test_login.php"; var postdata = {username : "tanmoy" , password : "123456"}; $http({ url: posturl, method: "post", data: postdata, crossdomain: true }) .then(function(result) { console.log("success", result); $scope.someval = json.stringify(result); }, function(response) { // optional console.log("error "+response); } ); }; });

i in problem. deep search .. cross-domain requests, setting content type other application/x-www-form-urlencoded, multipart/form-data, or text/plain trigger browser send preflight options request server. if server not allow throw errors. default angular content type application/json trying send alternative request. jquery default application/x-www-form-urlencoded; charset=utf-8 in case jquery working , angular not. seek overwrite angular default header . or allow in server end. here angular sample:

$http.post(url,data,{ headers : { 'content-type' : 'application/x-www-form-urlencoded; charset=utf-8' } });

see http://api.jquery.com/jquery.ajax/ content-type detail

javascript jquery ajax angularjs

No comments:

Post a Comment