简体   繁体   English

如何使用无名称的POST提交表单数据以获得唯一值

[英]How to submit form data using POST with no name for unique value

When generating a form using Javascript to post into an iframe, how to POST a single string as value without any attribute name attached to it? 使用Javascript生成要发布到iframe中的表单时,如何发布单个字符串作为值,而未附加任何属性名称?

I have this output (In Google Chrome console): 我有以下输出(在Google Chrome控制台中):

Form data: 表格数据:

data: "{'color': 'blue', 'shape': 'circle'}" 数据: “ {'颜色':'蓝色','形状':'圆形'}”

when I am actually looking for: 当我实际上正在寻找:

Form data: 表格数据:

"{'color': 'blue', 'shape': 'circle'}" “ {'颜色':'蓝色','形状':'圆形'}”

My code: 我的代码:

    var f = document.createElement("form");
    var i = document.createElement("input");
    i.setAttribute('type',"text");
    i.setAttribute('name',"data");
    i.setAttribute('value',"{'color': 'blue', 'shape': 'circle'}");
    f.appendChild(i);
    f.submit();

Note that I cannot use regular Xhr Calls at this is a cross domain task. 请注意,我不能使用常规的Xhr呼叫,因为这是跨域任务。

If I understand you correctly you are trying to deliver a data/json payload using an HTML form. 如果我理解正确,那么您正在尝试使用HTML表单传递data/json有效内容。 This is impossible, an HTML form can only send data with one of three encoding types (controlled by the enctype attribute on the form), application/x-www-form-urlencoded (the default format for POST), multipart/form-data (necessary when doing file uploads), or text/plain (obsolete, don't use this). 这是不可能的,HTML表单只能发送以下三种编码类型之一的数据(由表单上的enctype属性控制), application/x-www-form-urlencoded (POST的默认格式), multipart/form-data (在上传文件时是必需的)或text/plain (已过时,请勿使用此选项)。

Using AJAX won't help either, cross-domain AJAX is limited to doing exactly what an HTML form can, unless the server sends an Access-Control-Allow-Origin header. 使用AJAX也无济于事,除非服务器发送Access-Control-Allow-Origin标头,否则跨域AJAX只能做HTML表单可以做的事情。

If you have control over the server, you can change the format it expects the data in (or send an Access-Control-Allow-Origin , but that is supported by fewer browsers). 如果您可以控制服务器,则可以更改期望数据使用的格式(或发送Access-Control-Allow-Origin ,但较少的浏览器支持这种格式)。 If you don't, you can setup a simple API on the server you serve the Javascript from to pass requests through to the other server. 如果不这样做,则可以在服务Javascript的服务器上设置一个简单的API,以将请求传递到另一台服务器。 Even a simple PHP script would be enough. 即使是简单的PHP脚本也足够了。

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

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