Error making request via POST

Asked

Viewed 300 times

0

I am sending information to a page on my server via GET through my application, until then it is working normally. However, now I need to send an image in the format base64, I believe that sending via POST work well, but when I try to send I get the error:

Response with status: 0 for URL: null

I’ve even tried to remove the image from the parameters, but I get the same message.

In my configuration I added the proxy:

"proxies": [{
  "path": "/api",
  "proxyUrl": "https://meu_site.com.br/MeuProjeto/api/"
}]

NOTE: The link in the application is correct, only hidden here for privacy reasons.

In my provider is the call of methods get and post:

import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';

@Injectable()
export class ServiceProvider {

  urlApi: string = https://meu_site.com.br/MeuProjeto/api/';

  constructor(public http: Http) {

  }

    get(file: string){
      console.log("Requisição: " + this.urlApi + file);
      return this.http.get(this.urlApi + file).map(res => res.json());
    }

    post(file: string, params){
      let headers = new Headers( {
        'Access-Control-Allow-Origin' : '*',
        'Access-Control-Allow-Methods' : 'POST, GET, OPTIONS, PUT',
        'Content-Type': 'Application/x-www-form-urlencoded'
      });
      return this.http.post("/api" + file, params, {
        headers: headers
      }).map(res => res.json());
    }    
}

I call the post method as follows:

this.service.post('minha_pagina.php', params).subscribe(
  data =>{
  },
  err => {
  }
);

And then I get the error I mentioned at the beginning of the question. I have tried several ways to fix problems with CORS, but without success, below I will put my page that is receiving this request:

<?php
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT');
    header('Content-Type: Application/x-www-form-urlencoded');

    //Conteúdo da página.
?>
  • You really need to send this image in Base64 format?

1 answer

2

There is no HTTP status 0. This 0 means the return of your request. The second part of the error message says URL: NULL. One thing I noticed was the following excerpt just below exporting the Serviceprovider class:

 urlApi: string = https://meu_site.com.br/MeuProjeto/api/';

Where there is the first ' before HTTP. I do not know if you have already fixed it, but adjusting, would be like this:

 urlApi: string = 'https://meu_site.com.br/MeuProjeto/api/';

Another thing that might be is your proxy:

"proxies": [{
  "path": "/api",
  "proxyUrl": "https://meu_site.com.br/MeuProjeto/api/"
}]

Is it not concatenating proxyUrl with Path? Generating the following URL:

https://meu_site.com.br/MeuProjeto/api//api

(this end path/api).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.