HTML 表单方法是 POST 但它作为 OPTIONS 提交

纳吉布·伊哈布

至少可以说,我发生了一件奇怪的事情。

一个具有post何时提交方法的 html 表单正在使用options,这是 html 代码:

<form action="https://omitted" method="post" id="canvasForm">
    <div class="form-group" style="margin-top: 10%; text-align: center;">
        <h1>Canvas Integration</h1>
        <label style="text-align: right;" for="school_name" class="col-sm-4 control-label">School Prefix *</label>
        <div class="col-sm-10">
            <input type="text" style="margin-right: 1%; margin-bottom: 2%; width:60%" class="form-control" name="school_name" placeholder="School Prefix" tabindex="1" required>
            <!-- <i class="fa fa-question-circle" aria-hidden="true"></i> -->
        </div>
        <label style="text-align: right;" for="code" class="col-sm-4 control-label">Code *</label>
        <div class="col-sm-6" >
            <input id="code" type="text" class="form-control" style="margin-right: 1%; margin-bottom: 2%; width:60%" name="code" tabindex="1" placeholder="code" required>
        </div>
        <label style="text-align: right;" for="code" class="col-sm-4 control-label">School Canvas URL *</label>
        <div class="col-sm-6" >
            <input id="canvas_url" type="text" class="form-control" style="margin-right: 1%; margin-bottom: 2%; width:60%" name="canvas_url" tabindex="1" placeholder="school canvas url" required>
        </div>
        <label style="text-align: right;" for="clientid" class="col-sm-4 control-label">ID *</label>
        <div class="col-sm-6">
            <input type="text" class="form-control" style="margin-bottom: 2%;margin-right: 1%; width:60%" name="clientid" placeholder="ID" tabindex="1" required>
            <div class="tooltip">
                <i class="fa fa-question-circle" aria-hidden="true" >
                <img  class="tooltiptext" src="images/clientid.png" style="width: 550px"></i>
            </div>
        </div>
        <label style="text-align: right;" for="securitykey" class="col-sm-4 control-label">Key *</label>
        <div class="col-sm-6">
            <input type="text" class="form-control" style="margin-right: 1%; margin-bottom: 2%; width:60%" name="securitykey" placeholder="Key" tabindex="1" required>
            <div class="tooltip">
                <i class="fa fa-question-circle" aria-hidden="true" >
                <img  class="tooltiptext" src="images/securitykey.png" style="width: 550px"></i>
            </div>
        </div>
        <div class="col-sm-6">
            <input id="redirect_url" type="hidden" class="form-control" style="margin-right: 1%; margin-bottom: 2%; width:60%;float: center;" name="redirect_url" placeholder="Canvas URL" tabindex="1" required>
        </div>
    </div>
    <div class="form-group" style="text-align: center;">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-primary btn-w-md" >Submit</button>
        </div>
    </div>
    <div class="form-group" style="text-align: center;">
        <div id="response" class="col-sm-offset-2 col-sm-10">
        </div>
    </div>
</form>

我有一些提交表单的 JS 代码:

    form.onsubmit = function (e) {
        // stop the regular form submission
        e.preventDefault();

        // collect the form data while iterating over the inputs
        var data = {};
        for (var i = 0, ii = form.length; i < ii; ++i) {
          var input = form[i];
          if (input.name) {
            data[input.name] = input.value;
          }
        }
        // construct an HTTP request
        var xhr = new XMLHttpRequest();
        xhr.open(form.method, form.action, true);
        xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');

        // send the collected data as JSON
        xhr.send(JSON.stringify(data));

        xhr.onloadend = function (response) {
          console.log('response from server',response);
          var responseHtml = document.getElementById('response');
          if(response.target.status==400){
             responseHtml.innerHTML = '<div id="response" class="alert alert-success" role="alert" >' + response.target.response +'. Please contact edquire support team at [email protected].' + '</div>';
          } else if (response.target.status==200) {
             responseHtml.innerHTML = '<div id="response" class="alert alert-success" role="alert" >' + response.target.response +'! Your Canvas resource is successfully connected with us :)' + '</div>';
          } else {
             responseHtml.innerHTML = '<div id="response" class="alert alert-success" role="alert" >' + 'Something went wrong :( Please Try Again!' + '</div>';
          }
        };
    };

当我提交它时,我可以看到它正在使用options并且没有传递输入字段(检查下面的屏幕截图)在此处输入图片说明

亚历山大·埃尔金

这就是 CORS 的工作原理。您正在向远程主机提交 POST 请求。首先你的浏览器提交一个 OPTIONS 请求来检查远程主机是否接受来自其他主机的 HTTP 请求。如果是,则 POST 请求将在之后提交。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

HTML Imagemap作为表单提交

来自分类Dev

停止提交表单(在$ .post之后尝试e.preventDefault(),但它停止提交表单

来自分类Dev

如何使用HTML表单方法POST从数据库传递ID号

来自分类Dev

HTML表单方法GET

来自分类Dev

jQuery弹出窗口以post方法提交表单

来自分类Dev

带有POST方法的表单提交GET

来自分类Dev

提交HTML表单值作为数组(到PHP)

来自分类Dev

按钮作为提交表单

来自分类Dev

提交HTML文本表单?

来自分类Dev

HTML表单未提交

来自分类Dev

检测HTML表单提交

来自分类Dev

提交表单html django

来自分类Dev

表单未提交html

来自分类Dev

jQuery提交不带表单的POST

来自分类Dev

$ _POST表单提交后为空

来自分类Dev

提交POST表单laravel错误

来自分类Dev

HTML表单:仅使用HTML或CSS可以抑制提交的GET / POST参数吗?

来自分类Dev

在提交html表单上返回多个方法

来自分类Dev

带ServiceStack的HTML表单方法PUT

来自分类Dev

提交表单方法get时保留$ _GET ['p']

来自分类Dev

Laravel表单方法PUT无法检查单击了哪个提交

来自分类Dev

如何使用jquery $ .post()方法提交表单值

来自分类Dev

表单提交后POST方法返回404-Laravel

来自分类Dev

以HTML表单提交画布数据

来自分类Dev

Django表单未以html提交

来自分类Dev

在WebBrowser中提交HTML表单

来自分类Dev

HTML普通按钮提交表单

来自分类Dev

修改HTML表单提交输出

来自分类Dev

孤立的HTML表单未提交