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

1
  • 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. Commented Sep 12, 2014 at 8:31

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

do I need to do something in my controller where the data is submitted? Like sending HTTP code?
Explain why he should use then() instead of success()!
The status in the above indicates the response from the server, Here's a list of those 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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.