簡體   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