jQuery Ajax参数问题 [英] Problem with jQuery ajax parameters

查看:78
本文介绍了jQuery Ajax参数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以以下格式将日期发送到我的Web服务:12/14/2010,但"/"字符导致发布的值类似于0.0323483238432834.如何从字面意义上以这种格式发送日期?这是我使用的ajax代码:

I'm sending a date to my webservice in this format: 12/14/2010 but the "/" character is causing the posted value to be something like 0.0323483238432834. How do I literally send the date in that format? Heres the ajax code I use:

function createNewPromo() {
            var a = $("#txtDateStart").val();
            var b = $("#txtDateEnd").val();
            $.ajax({
                type: "POST",
                url: "/WebService_VehicleDisplay.asmx/createNewPromo",
                data: "{startDate:" + a + "," + "endDate:" + b + "}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    var response = msg.d;

                },
                failure: function (msg) {
                    alert('failure');
                }
            });
        } 

这是萤火虫说的话. endDate/startDate是我的参数.(这是发布到我的Web服务的东西.)

here is what firebug says. endDate/startDate are my parameters.(this is what is being posted to my web service.).

    JSON

endDate
    0.00021321961620469082

startDate
    0.00028429282160625445
Source
{startDate:12/21/2010,endDate:12/28/2010}

推荐答案

我已经弄清楚了,在参数值的每一侧都需要有一个单引号,如下所示:

I figured it out, there needs to be a single quote on each side of the parameter value like so:

data: "{startDate:'"+ a +"',endDate:'"+ b +"'}"

我在以下有关此问题的评论中写了一篇很棒的文章.这是其中的摘录:

再次,请记住,使用ASMX ScriptServices时必须具有application/json的Content-Type.通过在请求上设置Content-Type,您已承诺发送JSON序列化参数,并且URL编码的字符串与有效的JSON."

I included a great article in my comment below about this issue. Here is a snippet from it:

"Again, remember that a Content-Type of application/json is a requirement when working with ASMX ScriptServices. By setting that Content-Type on the request, you’ve committed to sending JSON serialized parameters, and a URL encoded string is far from valid JSON."

这篇关于jQuery Ajax参数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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