繁体   English   中英

在新选项卡中打开 azure web 应用程序 URL 并将帖子数据作为标题

[英]Open azure web app URL in new tab with the post data as headers

我有 azure web 应用程序,我想在新选项卡中打开它,并将帖子数据作为标题。 I have used below code using JavaScript which successfully open the azure web URL but having error as " The page you are looking for cannot be displayed because an invalid method (HTTP verb) is being used " and the different thing is that When I refresh the它打开的页面

$scope.openurl= function {
const URL = 'https://xyz.azurewebsites.net';
                const _data = {
                    token: "54165165165"
                };
                submit_post_via_hidden_form(URL, _data);
        };

        function submit_post_via_hidden_form(url, params) {
            var f = $("<form target='_blank' method='post' style='display:none;' id='form1'></form>").attr({
                action: url
            }).appendTo(document.body);

            for (var i in params) {
                if (params.hasOwnProperty(i)) {
                    $('<input type="hidden" />').attr({
                        name: i,
                        value: params[i]
                    }).appendTo(f);
                }
            }

            f.submit();

            f.remove();
        }

更新

由于您的项目没有服务器端代码。 我建议您将数据放入localstorage ,打开页面时可以使用它们$(document).ready(function(){ ###read data here from localstorgae### })

私人的

根据您的描述,我知道问题的原因。 首先,因为您发送post请求打开一个web页面并通过表单主体传输数据。 然后你的后端程序需要处理这个请求。

解决方法如下。 你可以下载我的演示 (.Net Core 3.1)

启动.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
        // Enable AllowSynchronousIO 
        services.Configure<IISServerOptions>(options =>
        {
            options.AllowSynchronousIO = true;
        });
    }

家庭控制器.cs

    public IActionResult Index()
    {
        StreamReader stream = new StreamReader(HttpContext.Request.Body);
        string body = stream.ReadToEnd();
        ViewData["id2"] = body;
        return View();
    }

索引.cshtml

@{
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
    <p>Test Data</p>
    <p>@ViewData["id2"]</p>
</div>

您还需要在项目中添加一个 class。

ReadableBodyStreamAttribute.cs

using Microsoft.AspNetCore.Authorization;
// For ASP.NET 3.1
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Filters;

namespace defaultPost
{
    public class ReadableBodyStreamAttribute : AuthorizeAttribute, IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationFilterContext context)
        {
            // For ASP.NET 3.1
            context.HttpContext.Request.EnableBuffering();
        }
    }
}

Mytest.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="Scripts/jquery.js"></script>
    <title>Document</title>
</head>
<script type="text/javascript">
    $(document).ready(function(){
        const URL = 'https://panshubeiweb.azurewebsites.net/';
                const _data = {
                    token: "54165165165"
                };
                openPostWindow(URL, _data);
    })

    function openPostWindow(url, params) {

      var newWin = window.open(),
            formStr = '';
       formStr = '<form style="visibility:hidden;" method="POST" action="' + url + '">' +
        '<input type="hidden" name="params" id="form2" value="' + params + '" />' +
        '</form>';
     newWin.document.body.innerHTML = formStr;
     newWin.document.forms[0].submit();

     return newWin;
  }
</script>
<body>  
</body>
</html>

您可以从 github 下载我的演示并进行测试。 还需要根据业务对body字符串( string body = stream.ReadToEnd(); )进行序列化。

在此处输入图像描述

暂无
暂无

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

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