Ashish Panwar Ashish Panwar - 1 year ago 115
jQuery Question

$ not working in AngularJS

When I use

my code works, but if I use $, I never get the parameters to the request .php file.

This is Service function:

TestPanel.service('MySampleService', function ($http, $q) {
this.getAllPosts = function () {

var def = $q.defer();
$'/data/AJAXRequest.php', 'mydata=1&abcd=2').success(function (data) {
if (data == null)
def.reject('ERROR: DATA IS NULL');
else if (data.HasError)
def.reject('ERROR: ' + data.Message);
}).error(function () {
def.reject('ERROR: Sorry, unable to complete your request.');

return def.promise;

And Controller function:

TestController.controller('PostCtrl', ['$scope', '$http', 'MySampleService',
function ($scope, $http, MySampleService) {

function FetchPostsList() {
MySampleService.getAllPosts().then(function (data) {
$scope.lstPosts = data.ResponseData;
$scope.totalRecords = data.totalRecords;'DATA=' + $scope.lstPosts);
function (err) {'err=' + err);


My AJAXRequest.php file


if I use $


array (size=0)

If i use $http.get()

my output is :

array (size=2)
'mydata' => string '1' (length=1)
'abcd' => string '2' (length=1)

I checked the post in FireBug tool, that its sending data to my php file. but php file getting no params.

If I use $.ajax or $.post my code work and it gives the response.

Answer Source

What if you specify the content-type in the headers, specifically like this:

method: 'POST',
url: '/data/AJAXRequest.php',
data: { mydata: 1, abcd: 2 },
headers: {'Content-Type': 'application/x-www-form-urlencoded'}

Found comments relating to PHP specifically from this question: AngularJs $ does not send data

It would seem that Angular sends as application/json by default, which can confuse PHP.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download