2

I tried to post username and password to api, but looks like it doesnt work as simple as jquery post. I keep geting this 400 error.

Code:

        $http({
            method: 'POST',
            url: apiLink + '/general/dologin.json',
            data: {"username":"someuser","password": "somepass"}
        }).success(function(response) {
            console.log(response)
        }).error(function(response){
            console.log(response)
        });

But if I add this line:

$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";

and change data to:

data: "username=someuser&password=somepass"

it works. But the thing is, that I have to use json.

And detailed informations from Google Chrome:

Request URL:http://coldbox.abak.si:8080/general/dologin.json
Request Method:POST
Status Code:400 Bad Request
Request Headersview source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en,sl;q=0.8,en-GB;q=0.6
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:57
Content-Type:application/x-www-form-urlencoded
Host:coldbox.abak.si:8080
Origin:http://localhost:8888
Referer:http://localhost:8888/
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)     Chrome/33.0.1750.154 Safari/537.36
Form Dataview sourceview URL encoded
{"username":"someuser","password":"somepass"}:
Response Headersview source
Access-Control-Allow-Origin:*
Connection:close
Content-Length:49
Content-Type:application/json;charset=utf-8
Date:Wed, 02 Apr 2014 07:50:00 GMT
Server:Apache-Coyote/1.1
Set-Cookie:cfid=b5bbcbe2-e2df-4eef-923f-d7d13e5aea42;Path=/;Expires=Thu, 31-Mar-2044     15:41:30 GMT;HTTPOnly
Set-Cookie:cftoken=0;Path=/;Expires=Thu, 31-Mar-2044 15:41:30 GMT;HTTPOnly
Clem
  • 9,476
  • 8
  • 31
  • 46
  • If you already solved your issue; did you have a question? You can set up a transform function so you won't have to worry about performing the conversion automatically. More info here http://www.jeffryhouser.com/index.cfm/2013/10/1/Calling-a-ColdFusion-CFC-from-AngularJS and here http://victorblog.com/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/ – JeffryHouser Apr 01 '14 at 14:02
  • My guess is that this is a server-side issue. The angular code looks correct. Perhaps the server is not configured to handle json parameters? – alexsanford1 Apr 01 '14 at 14:02

3 Answers3

2

I'm betting it's a CORS issue if your angular app isn't on the exact same domain as the server to which you're posting your JSON.

See this answer for details: AngularJS performs an OPTIONS HTTP request for a cross-origin resource

Community
  • 1
  • 1
Anders Bornholm
  • 1,266
  • 7
  • 17
  • 2
    Basically every POST to another server sends and OPTIONS request first that has to respond with the correct headers. If it doesn't respond, the browser will never even send the actual POST. But read the other answer for details. – Anders Bornholm Apr 02 '14 at 07:28
0

Try data: {username:"someuser",password: "somepass"} without the quotes around the username and password and see if that makes a difference.

lmyers
  • 2,614
  • 1
  • 22
  • 22
0

You would have to transform the data with a JSON.stringify when you assign that to the data

ajeeshpu
  • 190
  • 1
  • 2
  • 9