Re: $http.post(url, data) not working

23,123 views
Skip to first unread message

chris...@gmail.com

unread,
Jun 23, 2012, 2:43:20 PM6/23/12
to ang...@googlegroups.com
How about this:

$http({
url:url
data : data,
method : 'POST',
headers : {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
}).success(callback);

If you do not mind passing the data as url params, you can also try this:

$http.post(url+'?'+$.param(data)).success(callback);

Chris.

On Saturday, June 23, 2012 8:11:31 PM UTC+5:30, Josh Kamau wrote:
Hi guys , 

Please tell me why this code is not working.. Its jquery equivalent is working. 

//ng code... not working
$http.post(url, data).success(function(data, status){
           alert("Status:"+status+" Data:"+data.message);
}

//jquery code ... this is working
$.post(url, data, function(data, status){
            if(status == "success"){
                alert( "Data Saved: " + data.message );
           }

Now, both codes hit the database... but the data values are empty in case of angular code. 

What am i doing wrong? i want to use angular all the way and i feel bad when i have to fallback to jquery for ajax stuff.
Am using angular 1.0.0 

Josh.


Josh Kamau

unread,
Jun 24, 2012, 4:21:29 AM6/24/12
to ang...@googlegroups.com
I wanted to use the Shortcut method ($http.post) . 

The code is actually calling the post method in the server. But the data is empty. So i thought something is wrong with the way am passing the data. 

Josh.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/x2YanXyKSd4J.

To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.

Suller Andras

unread,
Jun 24, 2012, 5:15:28 AM6/24/12
to ang...@googlegroups.com
Angular works with Javascript objects by default, so when you write
$http.post(url, data), Angular assumes that "data" is a javascript
object or array. Probably in your case "data" is a string, so you need
to explicitly specify the content type of the data what you send.
That's what Chris advised.
The default is "Content-Type: application/json".
See "Setting HTTP Headers" section at http://docs.angularjs.org/api/ng.$http .

I recommend you to configure your server to accept JSON data. It makes
your life easier.

Andras

Josh Kamau

unread,
Jun 24, 2012, 5:25:19 AM6/24/12
to ang...@googlegroups.com
Here is the object am passing ..

 var data = {
            pyt : pyt,
            authorUserName : authorUserName,
            review : $scope.review,
            name : name,
            year : year,
            thumb : $scope.reviewThumb
        };

The same object works perfectly with jquery.

Josh.

Suller Andras

unread,
Jun 24, 2012, 5:39:24 AM6/24/12
to ang...@googlegroups.com
It looks like a javascript object, so we need more context to help
you. Can you create a workgin jsFiddle with your code? Or a gist?

Andras

Josh Kamau

unread,
Jun 24, 2012, 9:04:40 AM6/24/12
to ang...@googlegroups.com
Thanks... i will create a jsFiddle.

Josh.

Josh Kurz

unread,
Jul 9, 2012, 8:38:22 PM7/9/12
to ang...@googlegroups.com
+1 

On Thu, Jul 5, 2012 at 1:20 PM, Honey Darling <hdarling....@gmail.com> wrote:
I am very happy to report that I finally got a jsFiddle working with the /echo/json/ api.


A little jQuery param and a little JSON stringify does the trick.

Thanks for the tip, Jeremy, that's probably the other half of my problem.

Honey

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/LSkf_uHgVNAJ.

To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.



--
Josh Kurz

Head of Web Development
SEO
Social Media 
Digital Photography 
Internet Marketing

cell#      404-641-7807
office#    404-593-0851
fax#       404-506-9642
Dillinger Media 
http://dillingermediaonline.com


slim...@gmail.com

unread,
Jul 10, 2012, 8:16:23 AM7/10/12
to ang...@googlegroups.com
I'm just wondering if this is working as intended? If I'm POSTing to my back-end I would like to be able to access the data through $_POST and not going through 'php://input'.

On Wednesday, July 4, 2012 10:30:01 AM UTC+2, Jérémy moresmau wrote:
I've been struggling for a while to make an $http.post work with my CodeIgniter PHP framework. Actually you can't access your data by reading $_POST or $_GET. 
you need to go through "php://input", server-side.
This blog saved my day  : http://www.cleverweb.nl/javascript/a-simple-search-with-angularjs-and-php/

I hope it'll solve your problem,
Jeremy

harringt...@gmail.com

unread,
Nov 5, 2012, 1:05:19 AM11/5/12
to ang...@googlegroups.com, slim...@gmail.com
If you call angular.toJson(onYourPostDataObject) before you pass it to $http.post the data will be available at the backend via the  $_POST array.

ezekielja...@gmail.com

unread,
Dec 21, 2012, 7:02:33 AM12/21/12
to ang...@googlegroups.com
The confusion surrounding this issue inspired me to start that blog I've been meaning to write (Brian from Family Guy... anyone?). I wrote a post on this issue that includes a solution which will allow you to use $http service as expected without having to change server or client code:


Thank you for the kick ass framework!

- Zeke

yahya Kacem

unread,
Feb 20, 2013, 5:22:15 PM2/20/13
to ang...@googlegroups.com, jeremy....@gmail.com
Thank you very much, I was having the same problem and your post helped specially the link you provided.


On Wednesday, July 4, 2012 9:30:01 AM UTC+1, Jérémy moresmau wrote:
I've been struggling for a while to make an $http.post work with my CodeIgniter PHP framework. Actually you can't access your data by reading $_POST or $_GET. 
you need to go through "php://input", server-side.
This blog saved my day  : http://www.cleverweb.nl/javascript/a-simple-search-with-angularjs-and-php/

I hope it'll solve your problem,
Jeremy

yahya Kacem

unread,
Feb 21, 2013, 2:10:00 AM2/21/13
to ang...@googlegroups.com
Hi again, what about python, with php I used file_get_content('php://input'), but the app that I'm work on is with Python (pyramid) so how whould I retrieve the data send by angular's $http to the server.
Thanks in advance.

Danny Alvarez

unread,
Dec 12, 2014, 12:23:53 AM12/12/14
to ang...@googlegroups.com, jeremy....@gmail.com
Hey man, this is what I wanted, thanksssss


El miércoles, 4 de julio de 2012 02:30:01 UTC-6, Jérémy moresmau escribió:
I've been struggling for a while to make an $http.post work with my CodeIgniter PHP framework. Actually you can't access your data by reading $_POST or $_GET. 
you need to go through "php://input", server-side.
This blog saved my day  : http://www.cleverweb.nl/javascript/a-simple-search-with-angularjs-and-php/

I hope it'll solve your problem,
Jeremy
Reply all
Reply to author
Forward
0 new messages