首页 > 解决方案 > 在新选项卡中打开 azure web 应用程序 URL,将帖子数据作为标题

问题描述

我有 azure web 应用程序,我想在新选项卡中打开它,并将帖子数据作为标题。我使用以下代码使用 JavaScript 成功打开了 azure web URL,但出现错误“您正在查找的页面无法显示,因为正在使用无效的方法(HTTP 动词) ”,不同的是,当我刷新它打开的页面

$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();
        }

标签: javascriptazureazure-web-app-service

解决方案


更新

由于您的项目没有服务器端代码。建议你把你的数据放进去localstorage,打开页面就可以使用了$(document).ready(function(){ ###read data here from localstorgae### })

私人的

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

解决方法如下。你可以下载我的demo。(.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>

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

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();
        }
    }
}

我的测试.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 下载并测试它。您还需要string body = stream.ReadToEnd();根据业务对正文字符串( )进行序列化。

在此处输入图像描述


推荐阅读