简体   繁体   English

将表单序列化为json,不使用ajax

[英]Serialize form as json, without ajax

I'm trying to automate requests to some 3rd party site, which does not have an API, and no Access-control-Allow-Origin.我正在尝试自动请求某些 3rd 方站点,该站点没有 API,也没有 Access-control-Allow-Origin。 So no ajax possible, my only option is to reproduce the site's forms and post them.所以不可能有ajax,我唯一的选择是复制网站的表格并发布它们。

But on some forms, the site uses ajax with JSON encoding, and it seems the same data in url-encoded format is not accepted.但是在某些形式上,该站点使用带有 JSON 编码的 ajax,并且似乎不接受 url 编码格式的相同数据。

Application/json is not an accepted value of form's encType or formEncType attributes (thus is ignored when set). Application/json不是表单的encTypeformEncType属性的可接受值(因此在设置时被忽略)。 I've also tried building the object and serializing it in a hidden field (with encType as plain text), but a browser will ignore inputs without name, and putting a name will defeat the trick of passing a value as the request's content.我还尝试构建对象并将其序列化在隐藏字段中(使用 encType 作为纯文本),但浏览器将忽略没有名称的输入,并且输入名称将打败将值作为请求内容传递的技巧。

Is there a way in javascript to tell the browser to encode the data as json before sending it sychronously? javascript中有没有办法告诉浏览器在同步发送数据之前将数据编码为json?

You might be able to put some of the json in the name field, and the rest in the value field, so when you use enctype text/plain and the browser automatically adds a = after the name, it forms a valid json string.您也许可以将一些 json 放在 name 字段中,而将其余的放在 value 字段中,因此当您使用 enctype text/plain 并且浏览器自动在名称后添加一个 = 时,它会形成一个有效的 json 字符串。

The HTML spec says text/plain forms are made by writing the name, then a =, then the value, followed by a newline. HTML 规范说文本/纯格式是通过编写名称,然后是 =,然后是值,然后是换行符来制作的。 So if you split the json on the = and put the first part as the name and the second part as the value, it just might form a valid string that the server accepts.因此,如果您在 = 上拆分 json 并将第一部分作为名称,第二部分作为值,它可能会形成服务器接受的有效字符串。

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

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