似乎无法设置表单的编码类型. (Javascript) [英] Cannot seem to set a Form's Encoding Type. (Javascript)

查看:96
本文介绍了似乎无法设置表单的编码类型. (Javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对即时创建的表单进行硬连线,以使用编码类型"application/json"发送JSON.

I am trying to hardwire an on-the-fly created form to send JSON using the encoding type "application/json".

在jQuery中,我会在$.ajax$.post中将其设置为"contentType"-但是出于某些原因,我需要手动执行此操作.

in jQuery, I would set this as 'contentType' in an $.ajax or a $.post - however for certain reasons, I need to be doing this manually.

我有以下代码,但无法正常工作.仍然默认将enctype设置为application/x-www-form-urlencoded

I have the following code, but it just doesn't work. It still defaults the enctype to application/x-www-form-urlencoded

data = data;
var form = document.createElement("FORM");
form.style.display = "none";
form.action = url;
form.setAttribute('enctype', 'application/json');
form.method = "post";

我是要设置错误的属性,还是只是将其设置为错误?有什么想法吗?

Am I trying to set the wrong property, or am I just setting it wrong? Any ideas?

推荐答案

我认为form不能做到这一点.

I don't think a form can do that.

您需要在服务器端执行此操作.或者,如果您必须在发送之前在客户端上执行此操作(不建议这样做),请查看 JSON库

You'll need to do it on the server side. Or if you must do it on client prior to sending (not recommended) then look at a JSON library.

这篇关于似乎无法设置表单的编码类型. (Javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆