0

I created a simple submit page using codeigniter and angular.js. How can I throw HTTP status codes after somebody clicked submit button, so the the user will know if it is successful or not, or any other reason why it failed?

BTW, I base my code in this post Codeigniter + Angular Js: How to receive JSON data

Community
  • 1
  • 1
Port 8080
  • 858
  • 3
  • 12
  • 24
  • If it came to success callback, then it is succesful, if it come to error is not? Why do you want to know HTTP status code. For that you have to use then. – Chandermani Sep 12 '14 at 08:31

1 Answers1

0

Don't use .success() IMHO. Use .then() instead.

.then(function(resp){
    var $status = resp.status //http status code
    var $data = resp.data //data back from the server
});

If you're set on using success, then check out all the arguments for the success function:

success(function(data, status, headers, config)

As usual, check the docs when you'er in doubt

Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
  • do I need to do something in my controller where the data is submitted? Like sending HTTP code? – Port 8080 Sep 12 '14 at 08:27
  • Explain why he should use `then()` instead of `success()`! – Yang Sep 12 '14 at 08:28
  • The `status` in the above indicates the response from the server, [Here's a list of those codes](http://en.wikipedia.org/wiki/List_of_HTTP_status_codes). You can return your own response with custom text by simply returning a json encoded array, as such: `echo json_encode(array('status' => 'OK', 'msg' => 'it went ok!'));`. Then in your angular you can access this like: `resp.data.status` – Ohgodwhy Sep 12 '14 at 08:29
  • The reason I say to use `.then()` instead of `.success()` is due to the fact that a promise is returned from the `$http` call which can allow you to chain methods against it. In this particular case, it's a non issue, but in practice, it will be better as you'll be used to it for more complex tasks. – Ohgodwhy Sep 12 '14 at 08:30