1. <tfoot id='pM0zk'></tfoot>
      • <bdo id='pM0zk'></bdo><ul id='pM0zk'></ul>
    2. <i id='pM0zk'><tr id='pM0zk'><dt id='pM0zk'><q id='pM0zk'><span id='pM0zk'><b id='pM0zk'><form id='pM0zk'><ins id='pM0zk'></ins><ul id='pM0zk'></ul><sub id='pM0zk'></sub></form><legend id='pM0zk'></legend><bdo id='pM0zk'><pre id='pM0zk'><center id='pM0zk'></center></pre></bdo></b><th id='pM0zk'></th></span></q></dt></tr></i><div id='pM0zk'><tfoot id='pM0zk'></tfoot><dl id='pM0zk'><fieldset id='pM0zk'></fieldset></dl></div>

        <small id='pM0zk'></small><noframes id='pM0zk'>

        <legend id='pM0zk'><style id='pM0zk'><dir id='pM0zk'><q id='pM0zk'></q></dir></style></legend>

        Ionic 3 响应状态:0 对于 URL:null

        Ionic 3 Response with status: 0 for URL: null(Ionic 3 响应状态:0 对于 URL:null)
          <tbody id='86lgm'></tbody>
        <tfoot id='86lgm'></tfoot>

        • <bdo id='86lgm'></bdo><ul id='86lgm'></ul>

          1. <legend id='86lgm'><style id='86lgm'><dir id='86lgm'><q id='86lgm'></q></dir></style></legend>
              • <small id='86lgm'></small><noframes id='86lgm'>

                <i id='86lgm'><tr id='86lgm'><dt id='86lgm'><q id='86lgm'><span id='86lgm'><b id='86lgm'><form id='86lgm'><ins id='86lgm'></ins><ul id='86lgm'></ul><sub id='86lgm'></sub></form><legend id='86lgm'></legend><bdo id='86lgm'><pre id='86lgm'><center id='86lgm'></center></pre></bdo></b><th id='86lgm'></th></span></q></dt></tr></i><div id='86lgm'><tfoot id='86lgm'></tfoot><dl id='86lgm'><fieldset id='86lgm'></fieldset></dl></div>

                  本文介绍了Ionic 3 响应状态:0 对于 URL:null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 Ionic 3 应用程序.我最近添加了 iOS 平台.

                  I have an Ionic 3 app. I recently added the iOS platform.

                  当我在 iOS(模拟器和设备)上运行它时,所有具有标头的服务器请求都失败,并出现错误 响应状态:0 对于 URL:null".在 Android 上,这些请求可以正常工作.

                  When i run it on iOS (emulator and device) all the server requests that has headers fail with the error "Response with status: 0 for URL: null". On Android those requests works fine.

                  如果我执行请求没有标头,我会从服务器获得预期的响应.

                  If I do the requests without headers i get the expected response from server.

                  我知道问题出在 WKWebViewCORS 上.服务器已正确配置 CORS.我使用 @angular/http 模块进行请求.

                  I know the problem is with WKWebView and CORS. The server has the CORS configured correctly. I do the requests with @angular/http module.

                  让我们看一些代码.

                  这是我向服务器发出请求的提供者:

                  This is my provider for doing requests to server:

                  import { Injectable } from '@angular/core';
                  import { Content } from 'ionic-angular';
                  import { Http, Headers, RequestOptions, URLSearchParams } from '@angular/http';
                  import { HTTP } from '@ionic-native/http';
                  import { Observable } from 'rxjs/Observable';
                  import 'rxjs/add/operator/map';
                  import { Globalization } from '@ionic-native/globalization';
                  
                  import { Globals } from '../providers/globals';
                  
                  ...
                  
                  /// Here we have one example Request (it's inside a method)
                  
                  /// Here I create the URL for the request
                  let url = this.server_url + this.server_functions.doSearch;
                  
                  /// Now I create the Headers for the Request
                  let headers = new Headers();
                  /// As You can see, I have tried to pass diferent headers to server
                      // headers.append("Access-Control-Allow-Origin","*");
                      // headers.append("Origin", "https://localhost:8080");
                      // headers.append("Access-Control-Allow-Methods","POST, GET, OPTIONS, PUT");
                      // headers.append("Accept","application/json");
                      // headers.append("Content-Type","application/json; charset=utf-8");
                  
                  /// If the user is logged in I have to send this headers to server
                  if ( !this.globals.guestMode ) {
                      headers.append("TokenAuth", this.globals.getLogRegData()["TokenAuth"]);
                      headers.append("IdAuth", this.globals.getLogRegData()["IdAuth"]);
                  }
                  
                  /// And here we start the GET Request
                  this.http.get( url, { headers: headers } ).map(res => res.json()).subscribe(
                      data => {
                          // console.log( JSON.stringify(data) );
                          callback( data );
                      },
                      err => {
                          console.log("ELOL: "+err);
                      }
                  );
                  

                  另一方面,我决定尝试 @ionic-native/http 模块(正如您在导入中看到的那样)以避免 WKWebView 和 CORS 问题,但是当我使用它执行请求时,我收到了这个错误:

                  By the other way, I decided to try the @ionic-native/http module (as you can see in the imports) to avoid the WKWebView and CORS problems, but when I do the request with it, I got this error:

                  WARN: Native: tried calling HTTP.get, but the HTTP plugin is not installed.
                  WARN: Install the HTTP plugin: 'ionic cordova plugin add cordova-plugin-advanced-http'
                  

                  这就是我使用本机插件执行请求的方式:

                  This is how I do the Request with the native plugin:

                  this.httpnative.get(url, {}, {})
                      .then(data => {
                  
                          console.log(data.status);
                          console.log(data.data); // data received by server
                          console.log(data.headers);
                  
                      })
                      .catch(error => {
                  
                          console.log(error.status);
                          console.log(error.error); // error message as string
                          console.log(error.headers);
                  
                      });
                  

                  这是我的 app.module.ts 的片段:

                  This is a fragment of my app.module.ts:

                  import { HttpModule } from '@angular/http';
                  import { HTTP } from '@ionic-native/http';
                  ...
                  @NgModule({
                  ...
                   imports: [
                  ...
                      HttpModule,
                      HTTP,
                  ...
                  
                    ],
                  })
                  

                  我希望有人能给我一些启示,因为我迷失在 Ionic 的道路上.

                  I hope some one can bring me some light on this, because I'm so lost in the paths of Ionic.

                  谢谢.

                  推荐答案

                  为了避免 CORS 问题,特别是在 iOS 中,您必须使用 @ionic-native/http 插件,它是实际上是用于 API 调用的高级 HTTP 插件.

                  To avoid CORS problem specially in iOS you must use @ionic-native/http plugin which is actually Advanced HTTP plugin for API calling.

                  按照以下步骤使用此插件

                  Follow below steps to use this plugin

                  第一步:添加Http原生插件

                  Step 1: Add Http native plugin

                  $ ionic cordova plugin add cordova-plugin-advanced-http
                  $ npm install --save @ionic-native/http
                  

                  安装链接:HTTP

                  第 2 步:在您要调用 API 的文件中导入 HTTP 原生插件.

                  Step 2: Import HTTP native plugin in your file where you wants to cal API.

                  import { HTTP, HTTPResponse } from '@ionic-native/http';
                  

                  第 3 步:如何使用此插件进行 API 调用?

                  Step 3: How to use this plugin for API call ?

                  constructor(public httpPlugin: HTTP) {
                  
                    }
                  
                  //Set header like this
                  this.httpPlugin.setHeader("content-type", "application/json");
                  
                  //Call API 
                  this.httpPlugin.get(this.url, {}, {}).then((response) => {
                      //Got your server response
                  }).catch(error => {
                      //Got error 
                  });
                  

                  希望这会对你有所帮助.

                  Hope this will help you.

                  这篇关于Ionic 3 响应状态:0 对于 URL:null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  what#39;s property copy means in Cocoa#39;s Framework?(like UITabBar#39;s items property)(Cocoa 框架中的属性副本是什么意思?(如 UITabBar 的 items 属性))
                  Can`t copy file from bundle to documents directory in iOS(无法将文件从捆绑包复制到 iOS 中的文档目录)
                  Automatically copy property values from one object to another of a different type but the same protocol (Objective-C)(自动将属性值从一个对象复制到另一个类型不同但协议相同的对象 (Objective-C))
                  Copy text to clipboard with iOS(使用 iOS 将文本复制到剪贴板)
                  How to copy text to clipboard/pasteboard with Swift(如何使用 Swift 将文本复制到剪贴板/粘贴板)
                  How to show the progress of copying a large file in iOS?(如何在iOS中显示复制大文件的进度?)
                    <tbody id='sW0DC'></tbody>
                  • <legend id='sW0DC'><style id='sW0DC'><dir id='sW0DC'><q id='sW0DC'></q></dir></style></legend>
                    • <tfoot id='sW0DC'></tfoot>
                      1. <small id='sW0DC'></small><noframes id='sW0DC'>

                            <bdo id='sW0DC'></bdo><ul id='sW0DC'></ul>

                          • <i id='sW0DC'><tr id='sW0DC'><dt id='sW0DC'><q id='sW0DC'><span id='sW0DC'><b id='sW0DC'><form id='sW0DC'><ins id='sW0DC'></ins><ul id='sW0DC'></ul><sub id='sW0DC'></sub></form><legend id='sW0DC'></legend><bdo id='sW0DC'><pre id='sW0DC'><center id='sW0DC'></center></pre></bdo></b><th id='sW0DC'></th></span></q></dt></tr></i><div id='sW0DC'><tfoot id='sW0DC'></tfoot><dl id='sW0DC'><fieldset id='sW0DC'></fieldset></dl></div>