简体   繁体   English

方法中的Http.post和Ionic Framework问题

[英]Http.post and Ionic Framework problems in method

I am developing an application with Ionic Framework Version 3.19.1, I am making a request via post, the URL data, and necessary parameter information is all ok, however, it is returning an error that I can not solve, I have tried many ways, imports into the project, but without success.我正在使用 Ionic Framework Version 3.19.1 开发一个应用程序,我正在通过 post 发出请求,URL 数据和必要的参数信息都可以,但是,它返回一个我无法解决的错误,我尝试了很多方式,导入到项目中,但没有成功。 below is my post function.下面是我的帖子功能。

const req = this.http.post(url, {      
      options: {
        headers:[header],
        params:[postData]
      }
    }).subscribe(
      res => {
        console.log(res);
      },
      err => {
        console.log('Ocorreu um erro');
      }
    )

Below are my imports inside the .ts file (TypeScript)以下是我在 .ts 文件(TypeScript)中的导入

import { Component } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { IonicPage, NavController, ToastController } from 'ionic-angular';
import { HttpClient, HttpParams, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
import { IonicStorageModule } from '@ionic/storage';

import { User } from '../../providers/providers';
import { MainPage } from '../pages';

Well, as I said I'm doing a post request and on the console, it returns an OPTIONS 500 (Internal Server Error)好吧,正如我所说,我正在执行发布请求,并且在控制台上,它返回一个 OPTIONS 500(内部服务器错误)

Failed to load (URL): Response to preflight request does not pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http: // localhost: 8100' is therefore not allowed access. The response had HTTP status code 500.

by what I understand is reporting a problem regarding the Header, but I have already informed the correct one and left the requests open, but it still does not work, here is my header below.据我所知,正在报告有关标题的问题,但我已经通知了正确的问题并让请求保持打开状态,但它仍然不起作用,下面是我的标题。

const header = new HttpHeaders();
    header.set('Access-Control-Allow-Origin', '*');
    header.set('Content-type', 'application/json');

change the const req更改常量请求

return new Promise((resolve, reject) => {

        this.http.post(url, postData,
          {headers: this.header})
          .subscribe(
            data => {
              console.log('success');
              resolve(data);
            },
            err => {
              reject(err);
              console.log(err);}
          );
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM