Vue-resource: Response Object always have '0' on status

Created on 26 Jan 2016  路  6Comments  路  Source: pagekit/vue-resource

Hello ppl.

Im getting status code 0 on any HTTP error. On chrome network tab says 409, 422 but the object come whit the wrong status.
Tks !

Code:

  this.$http.post('http://localhost/sigmapi/public/api/' + this.apiName, {nome: this.novoNome})
.then(function (response) {
this.dados.push(response.data['data'])
this.novoNome = ""
swal({
title: "Criado",
text: "Voc锚 criou a configura莽茫o " + response.data['data'].nome + "",
html: true,
type: "success"});
}, function (response) {
if(response.status == 409){
var errorMsg = "Desculpe, mas a configura莽茫o " + this.novoNome + " j谩 existe.";
}
if(response.status == 422){
var errorMsg = "O nome da nova configura莽茫o n茫o pode ser vazio."
}
swal("Erro", errorMsg, "error");
});

Most helpful comment

I understand my problem. It was CORS not enable for http 400 family.
Tks all

All 6 comments

I understand my problem. It was CORS not enable for http 400 family.
Tks all

@odranoelBR excuse me锛宑ould you tell me how you fix this problem锛焧hank you!

Hi @rongself !
This problem comes from the API / WebService you tryng to request.

Take a read in https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

You must enable cross realm on your API.

  1. The browser willl make an HTTP OPTION before the real request
  2. In this OPTION response will be present all the headers the API accept.
    image
    This photo resonse says the API 'allow' this HTTP methods , aceppt no ranges etc
  3. So on your API enable everything you want.

You building the API or using third ?

@odranoelBR
Thank you! this is a lot of help for me , I am building the API use Laravel,this is the code i set the response header:

/**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $response = $next($request);

        $httpOrigin = $request->header('Origin');
        if ($httpOrigin && in_array($httpOrigin, explode(',', env('CORS_DOMAINS')))) {

            $response->header('Access-Control-Allow-Origin', $httpOrigin);
            $response->header('Access-Control-Allow-Credentials', 'true');
            $response->header('Access-Control-Expose-Headers', 'Authorization');//鍏佽鏈嶅姟鍣ㄦ帴鏀舵寚瀹氱殑瀹㈡埛绔彂閫佽繃鏉ョ殑鑷畾涔塰eader

            if($request->getMethod() === Request::METHOD_OPTIONS){
                $response->header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE');
                $response->header('Access-Control-Allow-Headers', 'Authorization');//鍏佽娴忚鍣ㄥ彂閫佹寚瀹氱殑鑷畾涔塰eader
            }

        }

        return $response;
    }

so now the thing i need to to is set Public and Allow these response headers right?

@odranoelBR
I understand my problem,i stop the laravel framework when login failed:

abort(Response::HTTP_UNAUTHORIZED,$e->getMessage()); 

i think this will break the response,so i change it to :

return Response::create($e->getMessage(),Response::HTTP_UNAUTHORIZED);

now it work , thanks you and have a nice day

@rongself excuse me, i have some problem,I am front web dev , and back end framework is laravel. In some
phone(wechat 6.5.6,andorid 6.0, honor 7) will arise status that is 0. And Exclusion of cross-domain problem.
can you help me?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ayyobro picture ayyobro  路  3Comments

mikeyao picture mikeyao  路  6Comments

facesea picture facesea  路  5Comments

laizhenhai88 picture laizhenhai88  路  4Comments

santigraviano picture santigraviano  路  5Comments