具有多个标头的 Ionic 4 原生 http POST

2023-12-04

以下是我从 Ionic 应用程序执行 http post 的方式:

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

someAction(assetId: number, userId: number)
{
    var asset: Asset = new Asset();
    asset.assetId = assetId.toString();
    asset.userId = userId.toString();

    const options = {
       headers:  "Content-Type: application/json; charset=utf-8"
    };

    return this.http.post(this.url + "someAction", asset, options).then(data => {
        return "Done";
    }).catch(err => {
        console.log(err);            
    });
}

根据这里的文档 -https://www.npmjs.com/package/cordova-plugin-advanced-http#post,标头应该像这样发送:

标题:{'内容长度':'247'}

这对我不起作用。请注意我如何将标头键和值作为一个字符串发送:

标题:“内容类型:application/json;字符集=utf-8”

这是错误 -

错误:高级-http:标头值必须是字符串 在 checkKeyValuePairObject (helpers.js:88) 在 checkHeadersObject (helpers.js:192) 在 Object.handleMissingOptions (helpers.js:309) 在 Object.sendRequest (public-interface.js:143) 在 Object.post (public-interface.js:167) 在 callCordovaPlugin (vendor.js:76774) 在供应商.js:76589 在供应商.js:76547 在新的 ZoneAwarePromise (polyfills.js:3268) 在 tryNativePromise (vendor.js:76546)

尝试发送多个标头时出现相同的错误。我不知道为什么会发生这种情况 - 文档中的示例不起作用!我缺少什么?

编辑:web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<connectionStrings>
    <add name="Default" connectionString="..." />
</connectionStrings>

<appSettings>
    ...
</appSettings>

<system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" maxRequestLength="75096" executionTimeout="600" />
</system.web>

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

    <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
    </dependentAssembly>
    <dependentAssembly>
        <assemblyIdentity name="System.IdentityModel.Tokens.Jwt" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
    </dependentAssembly>
    <dependentAssembly>
        <assemblyIdentity name="Microsoft.IdentityModel.Protocol.Extensions" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.0.40306.1554" newVersion="1.0.40306.1554" />
    </dependentAssembly>
    <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" />
    </dependentAssembly>
    <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" />
    </dependentAssembly>
    <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Jwt" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" />
    </dependentAssembly>
    <dependentAssembly>
        <assemblyIdentity name="Microsoft.IdentityModel.Tokens" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
    </dependentAssembly>
    <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.6.0" newVersion="5.2.6.0" />
    </dependentAssembly>
    <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.6.0" newVersion="5.2.6.0" />
    </dependentAssembly>
    <dependentAssembly>
        <assemblyIdentity name="Autofac" publicKeyToken="17863af14b0044da" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.8.1.0" newVersion="4.8.1.0" />
    </dependentAssembly>
    <dependentAssembly>
        <assemblyIdentity name="System.Web.Http.WebHost" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.6.0" newVersion="5.2.6.0" />
     </dependentAssembly>
     <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
    </dependentAssembly>

    <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
    </dependentAssembly>
    <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
    </dependentAssembly>
    <dependentAssembly>
        <assemblyIdentity name="System.Web.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
    </dependentAssembly>
    <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
    </dependentAssembly>
    <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
    </dependentAssembly>
</assemblyBinding>
</runtime>

<system.codedom>
    <compilers>
        <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
        <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
</system.codedom>

<system.webServer>

<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true">
  <remove name="FormsAuthentication" />
  <remove name="WebDAVModule" />
</modules>

<handlers>
  <remove name="WebDAV" />
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>

<security>
  <requestFiltering>
    <verbs allowUnlisted="false">
      <clear />
      <add verb="GET" allowed="true" />
      <add verb="POST" allowed="true" />
      <add verb="DELETE" allowed="true" />
      <add verb="PUT" allowed="true" />
      <add verb="OPTIONS" allowed="true" />
    </verbs>
  </requestFiltering>
</security>
<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
  </customHeaders>
</httpProtocol>

</system.webServer>

<log4net>
...
</log4net>

</configuration>

你的标题应该是一个对象:

const options = {
    headers: {
        'Content-Type': 'application/json'
    }
};

此外,您很可能需要启用 CORS 标头。根据您的 Web API 版本,您可以尝试以下方法之一:使用 WebAPI PUT / POST 请求启用 CORS? or https://learn.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

具有多个标头的 Ionic 4 原生 http POST 的相关文章

  • iOS 状态栏背景颜色

    我很难将 iOS 状态栏背景颜色设置为透明 我正在使用最新版本的 cordova s tatusbar 和 ionic 并将所有内容保留为默认设置 但是由于某种原因我得到了白色背景颜色 我如何设置要移植的背景颜色 尽管这是默认值 好的 参考
  • ionic-2 本机 youtube 视频播放器插件安装失败?Cordova 错误:无法获取插件?

    你好 我正在构建一个 ionic 2 应用程序 它显示一些 YouTube 视频内容 我研究了 ionic 本机文档https ionicframework com docs native youtube video player http
  • 使用 typeScript 滚动到 webView 上的 x,y 坐标

    我正在我的应用程序中制作自定义地图 这本质上是一个大地图图像 我根据 GPS 位置在大地图图像上移动一个小头像图像 我允许用户滚动地图以查看屏幕外的地方 我现在想添加一个按钮 使用户回到他们的位置中心 但它不起作用 我尝试使用 window
  • 即使添加控制台插件后,console.log 在 iOS Phonegap 应用程序中也不起作用

    我在 Cordova CLI 中创建了一个 Phonegap iOS 应用程序 我已经添加了控制台插件 是的 deviceready 被成功调用 但是 console log 不起作用并且不会在 XCode 日志中打印任何内容 插件安装 科
  • Ionic 4 - 删除滚动条

    在 Ionic 4 中删除滚动条同时仍然保持滚动和使用影子 DOM 的能力的最佳方法是什么 webkit scrollbar webkit scrollbar display none 上面这个没有效果 试试这个 它对我来说在 ionic
  • 如何为 Ionic 4(android / ios)应用程序创建 google pay?

    我使用以下方法创建了 google pay for web URL Google 网络付费 https developers google com pay api web guides tutorial 我想为 ionic 4 应用程序创建
  • Ionic React 4.11:如何在导航中推送和弹出 (IonNav)

    我是离子的新手 我刚刚安装了新发布的 Ionic React 4 11 https ionicframework com blog announcing ionic react https ionicframework com blog a
  • 使用 Cordova 插件拍摄全景图像?

    是否可以使用某些 cordova 插件 在 X 轴和 Y 轴 拍摄全景图像 感谢您的任何建议 相机插件启动设备的相机应用程序 所以我想在带有全景相机应用程序的手机上 您可以从科尔多瓦应用程序中拍摄全景图 用户必须手动选择该功能 否则你必须要
  • Ionic2,如何将自定义插件(appsee 或 uxcam)导入到 Ionic App 中

    我正在尝试使用Appsee https www appsee com docs ios ionic or UXcam https uxcam com docs 我尝试像下面这样导入 但没有成功 从 ionic native appsee 导
  • 在 Cordova 应用程序中播放音频

    我正在使用 Cordova 媒体插件在我的移动应用程序中播放音频 我尝试了很多代码 但我没有弄清楚我在底部做错了什么 我放了两段我尝试过的代码 第一个代码 js代码在单独的文件中 var app initialize function th
  • 如何将 Font Awesome 添加到 Ionic 4

    有很多关于如何将 Font Awesome 添加到 Ionic 3 项目中的教程和文章 但我很难找到有关如何将 Font Awesome 添加到 Ionic 4 项目中的教程和文章 那么这就提出了一个问题 如何在 Ionic 4 项目中添加
  • 使用 org.apache.cordova.file 获取选定的文件大小

    使用 org apache cordova file 插件 我可以选择该文件并获取该文件的本机路径 之后 我必须限制用户根据文件大小选择文件 但我无法理解文件大小 我的问题是我无法使用该插件获取文件大小 为此我正在使用本教程 http th
  • Ionic 4 网络检查

    如何检查 Ionic 4 中每个页面的互联网连接 需要的是 如果出现网络错误 我需要将应用程序重定向到错误页面 您可以尝试官方的 Ionic Network 插件 dochere https ionicframework com docs
  • Ionic Cordova Build Android 未找到已安装的构建工具

    我在 Ubuntu 16 04 上的 Android 上构建 ionic 应用程序时遇到问题 这是我为了实现这一目标所做的一步 下载Android Studio 下载Android SDK 在 bashrc 和 bash profile 中
  • Cordova 插件包括 Android 库 (Gradle)

    我想包括这个 Androidlibrary https github com 50ButtonsEach fliclib android在 Cordova 插件中 该库本身由 Gradle 管理 如 Github 链接所示 图书馆的开发者只
  • 无法让 Cordova 文本转语音插件工作

    我正在尝试各种 TTS 插件 包括位于https github com vilic cordova plugin tts https github com vilic cordova plugin tts 但无法让任何工作 例如 根据文档
  • cordova-plugin-whitelist 适用于 Android,但不适用于 iOS (Phonegap Build)

    我正在开发一个用 Cordova 封装并使用 Phonegap Build 构建的 JavaScript 应用程序 我们包括cordova plugin whitelist来自我们构建中的 npm 并添加了
  • 用一个命令更新 cordova 插件

    我想知道是否有更简单的方法来更新 cordova 插件 我用谷歌搜索 找到了一个钩子 2013年 但这不是100 我想要的 我知道我可以通过两个步骤来做到这一点 rm 然后添加 但我正在寻找更好的 官方 方法来帮助我哪些插件有更新的版本 我
  • 如何获取PhoneGap将看到的android目录中的文档

    我希望能够将一些文件复制到我的 PhoneGap Cordova 的 Documents 目录中 以便当我使用 cordova plugin file API 列出该目录时它们会显示出来 不幸的是 文件 API 与平板电脑存储上的实际内容之
  • 从设备读取 ionic www/config.xml 文件

    我在从 Android 设备读取 www config xml 文件时遇到问题 我尝试了两种不同的方法 首先 我尝试使用另一个线程中推荐的 http 服务 当这不起作用时 我尝试使用 Cordova 文件插件 我这样做的原因是尝试从应用程序

随机推荐