简体   繁体   English

jQuery Ajax发布到WCF的请求越来越糟糕

[英]jQuery Ajax post to WCF getting bad request

I am attempting to create an auto-complete box... and I can retrieve appropriate JSON using Fiddler, but when implemented in code, I get a connection error. 我试图创建一个自动完成框...,我可以使用Fiddler检索适当的JSON,但是在代码中实现时,出现连接错误。 Code: 码:

<htm>
<Head>
</head>
<body>
<input type="text" id="txt_search" name="search">
<span id="suggest"></span>      
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {

    $("#txt_search").keyup(function() 
    {

        var search; 
        search = $("#txt_search").val(); 

        if (search.length > 2) 
            { 

                // Trigger AJAX request 

                $.ajax( 
                { 
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "*****",
                    data: { "FirstPartOfName":"Med " },
                    dataType: "json",
                    success: function(message)  { 
                        if (message.length > 0) 
                            { 
                            alert('It got data back....');
                            message = "Do you mean: " + message; 
                            $("#suggest").append(message);
                            }
                        else
                            {
                            alert('Nothing came back....');
                            }
                        }
                }       );
            } 
            else { 
                // Empty suggestion list 
                $("#suggest").empty();
                } 
    });

});
</script>
</body>
</html>

I cannot provide the URL, but I can connect using Fiddler to test it. 我无法提供URL,但是我可以使用Fiddler进行连接进行测试。 I'm thinking it could be a problem with WCF, but how then can I test correctly with Fiddler? 我以为WCF可能是个问题,但是我怎样才能用Fiddler正确测试呢?

I don't know if this is your primary problem here, but by setting the contentType to application/json , you're telling WCF that you're sending it parameters in JSON serialized format, but when you pass a native object to jQuery like that, it's going to URL encode those parameters instead. 我不知道这是否是您的主要问题,但是通过将contentType设置为application/json ,您告诉WCF您正在以JSON序列化格式发送参数,但是当您将本地对象传递给jQuery时那,它将改为URL编码这些参数。 In other words, you're sending ?FirstPartOfName=Med instead of {"FirstPartOfName":"Med"} . 换句话说,您要发送?FirstPartOfName=Med而不是{"FirstPartOfName":"Med"} More about that here: http://encosia.com/asmx-scriptservice-mistake-invalid-json-primitive/ 有关此的更多信息,请访问: http : //encosia.com/asmx-scriptservice-mistake-invalid-json-primitive/

In this simple case, you can fix that by explicitly sending a JSON string instead: 在这种简单情况下,您可以通过显式发送JSON字符串来解决此问题:

data: '{"FirstPartOfName":"Med"}'

It looks similar, but is completely different over the wire. 它看起来很相似,但是在导线上却完全不同。

Building the JSON string by hand is workable enough in the simplest case, but gets to be cumbersome. 在最简单的情况下,手动构建JSON字符串是足够可行的,但是麻烦。 You can also use JSON.stringify to automatically build the string from objects instead. 您还可以使用JSON.stringify来自动根据对象构建字符串

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

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