简体   繁体   English

Ajax发布请求字符串数据而不是对象

[英]Ajax post request string data instead of object

Similar to this question I need to send a string as data in a post request. 此问题类似,我需要在帖子请求中发送一个字符串作为数据。

Unlike that one, I can't use an object because I have repeated items. 与那个不同,我不能使用一个对象,因为我有重复的项目。 As you can see in my sample data sn1, sn2 and sn3 are repeated several times on different datetimes. 正如您在我的示例数据sn1中看到的那样,sn2和sn3在不同的日期时间重复多次。

Sample data: 样本数据:

&sn3=2013-2-4T12:43:52&sn3=2013-2-4T12:43:55&sn1=2013-2-4T12:43:59&sn1=2013-2-4T12:44:0&sn2=2013-2-4T12:44:0&sn3=2013-2-4T12:44:2&sn2=2013-2-4T12:44:3&sn3=2013-2-4T12:44:19&sn3=2013-2-4T12:44:19&sn3=2013-2-4T12:44:19&sn2=2013-2-4T12:44:19&sn3=2013-2-4T12:44:21&sn2=2013-2-4T12:44:22&sn2=2013-2-4T12:46:39&sn3=2013-2-4T12:46:42&sn2=2013-2-4T12:46:44&sn2=2013-2-4T12:46:45&sn2=2013-2-4T12:46:46&sn2=2013-2-4T12:47:27&sn2=2013-2-4T12:47:27&sn2=2013-2-4T12:49:44&sn2=2013-2-4T12:50:21&sn2=2013-2-4T12:52:21&sn2=2013-2-4T12:52:24&sn2=2013-2-4T12:57:35&sn3=2013-2-4T12:57:38&sn3=2013-2-4T12:57:39&sn2=2013-2-4T12:57:39&sn2=2013-2-4T12:57:40&sn3=2013-2-4T12:57:46&sn3=2013-2-4T13:21:30

I tried using the following 我尝试使用以下内容

console.log(screens); //logs my sample data posted above.
        $.ajax({
            url : url,
            type: "POST",
            dataType : 'text',
            data : screens,
            success : function(data) {
                console.log("sucessfull sending:")
                console.log(data);
            },
            error : function() {
                console.log('failed');
            }

        });

But it always triggers failed. 但它总是触发失败。

Can I send it as a string? 我可以把它作为字符串发送吗? If not, how can I send multiple items with the same key? 如果没有,我如何使用相同的密钥发送多个项目?

    console.log(screens); //logs my sample data posted above.
    $.ajax({
        url : url,
        type: "POST",
        dataType : 'text',
        data : {screens:screens},
        success : function(data) {
            console.log("sucessfull sending:")
            console.log(data);
        },
        error : function() {
            console.log('failed');
        }

    });

See data : {screens:screens}, , if you do something like that, on server you will be able to get it like: screensString = Request["screens"] . 查看data : {screens:screens},如果你这样做,在服务器上你可以得到它: screensString = Request["screens"] After that, screensString will contain a single string with all screens. 之后,screensString将包含一个包含所有屏幕的单个字符串。

When you don't specify contentType in the ajax options, your request will default to 'application/x-www-form-urlencoded; 如果未在ajax选项中指定contentType,则您的请求将默认为'application / x-www-form-urlencoded; charset=UTF-8'. 字符集= UTF-8' 。 However, when your post data is just text, you should make the server aware of that fact by specifying a contentType 'text'. 但是,当您的帖子数据只是文本时,您应该通过指定contentType“text”让服务器知道这一事实。 As opposed to the contentType, the dataType specifies the type of response data that you expect back from the server. 与contentType相反,dataType指定您希望从服务器返回的响应数据的类型。

I think what you need is to use [] in your parameters. 我认为你需要的是在你的参数中使用[]。

instead of sending &sn3= multiple times (which is rewriting itself) send it as an array like this &sn3[]= 而不是发送&sn3=多次(这是重写本身)发送它像这样的数组&sn3[]=

if you are getting this data from an form input use name="sn3[]" and if this is the case, I would recommend you use $('#yourform').serialize() as data sent 如果从表单输入中获取此数据,请使用name="sn3[]" ,如果是这种情况,我建议您使用$('#yourform').serialize()作为数据发送

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

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