简体   繁体   English

jQuery使用json wcf C#服务

[英]jQuery consuming a json wcf C# service

This is what my service looks like. 这就是我的服务。
I have a class and an Interface. 我有一个类和一个接口。 My services is exposed and I can call them via the wcftestClient tool. 我的服务已公开,可以通过wcftestClient工具调用它们。

Am I able to consume the web service like this with my jQuery ? 我可以使用jQuery使用这种Web服务吗?

The problem I have is calling it in jQuery. 我有的问题是在jQuery中调用它。 It returns in JSON format. 它以JSON格式返回。 I get the following error in firebug. 我在萤火虫中收到以下错误。

Status Code: HTTP/1.1 404 Not Found 状态代码:找不到HTTP / 1.1 404

.cs .cs

public static string Serialize(object obj)
        {

            if (obj == null)
            {
                string temp = "";
                temp = "";
                obj = temp;
            }

            var jsonSerializer = new DataContractJsonSerializer(obj.GetType());
            string returnValue = "";
            using (var memoryStream = new MemoryStream())
            {
                using (var xmlWriter = JsonReaderWriterFactory.CreateJsonWriter(memoryStream))
                {
                    jsonSerializer.WriteObject(xmlWriter, obj);
                    xmlWriter.Flush();
                    returnValue = Encoding.UTF8.GetString(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
                }
            }
            return returnValue;
        }

        public string HelloWorld()
        {
            return "Hello World";
        }

Interface 接口

 [OperationContract]        
        string HelloWorld();

jQuery jQuery的

 function GetService() {
                            var BizName = $('#txtBizName').val();
                            //alert(BizName);
                            $.ajax({
                                url: "http://www.website.com/WCFService/EdWebService.svc?HelloWorld",
                                type: "GET",
                                dataType: "json",                               
                                contentType: "application/json; charset=utf-8",
                                success: function(msg) {
                                    $('#status').html('Output: '+msg['d']['Id']);
                                },
                                error: function(e) {
                                    $('#status').innerHTML = "Unavailable";
                                }                               
                            });

                            alert(BizName);
                        }

You should replace url: "http://www.website.com/WCFService/EdWebService.svc?HelloWorld" with url: "http://www.website.com/WCFService/EdWebService.svc/HelloWorld" at first. 此时应更换url: "http://www.website.com/WCFService/EdWebService.svc?HelloWorld"url: "http://www.website.com/WCFService/EdWebService.svc/HelloWorld"在第一。

Reason: you are not calling HelloWorld method, instead you pass 'HelloWorld' as parameter (in a very wrong way) to some unknown method (I don't know to which method resolves service name without method specified) of your service. 原因:您不是在调用HelloWorld方法,而是将“ HelloWorld”作为参数(以非常错误的方式)传递给您的服务的某个未知方法(不知道未指定方法的情况下,哪个方法可以解析服务名称)。

Then it should work. 然后它应该工作。 Please update your question if it doesn't. 如果不是,请更新您的问题。

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

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