返回JSONP以适当的格式WCF [英] Return JSONP in proper format WCF

查看:234
本文介绍了返回JSONP以适当的格式WCF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图输出正确包裹JSONP值jQuery的消耗

I'm trying to output a correctly wrapped JSONP value for jQuery to consume.

我在寻找的输出是:

jsoncallback({"Status": "OK"})

但它输出的大气压:

"jsoncallback({"Status": "OK"})"

显然,这不是正确的JSONP格式,jQuery的请求不能处理响应。

Evidently this is not in correct JSONP format as the jQuery request can't handle the response.

我在C#OperationContract的是:

My OperationContract in C# is:

[OperationContract]
[WebInvoke(Method = "GET",
   ResponseFormat = WebMessageFormat.Json,
   UriTemplate = "returndata?s={s}")]
Stream EchoWithGet(string s);

    public string EchoWithGet(string s)
    {
        string json = @"jsoncallback({'Status':'OK'})";
        Console.WriteLine("Call Made: " + s);
        return json;
    }



我试过使用 JSON.NET ,并在 System.Web.Script 命名空间使用的JavaScriptSerializer

I've tried using JSON.NET and also the System.Web.Script Namespace to use the JavaScriptSerializer.

但是,没有什么工作对我来说一切其实我想要做的是摆脱两个双引号的。

But nothing is working for me all I actually want to do is get rid of two double quotes.

推荐答案

如果您要提交的jQuery Ajax请求,并要求数据类型:JSONP,jQuery将传递一个名称为在请求回调函数(例如, / returndata S =你好&放大器;?回调= jquery123456789 ),所以返回一个常数jsonCallback不会在这种情况下正常工作

If you're submitting an ajax request with jQuery and asks for dataType: "jsonp", jQuery will pass a name for the callback function in the request (e.g., /returndata?s=hello&callback=jquery123456789), so returning a constant "jsonCallback" will not work in that case.

另外,在你的问题你有经营合同的定义返回,而在操作本身你返回字符串 - 什么是错的有

Also, in your question you have the operation contract definition returning Stream, while on the operation itself you're returning string - something is wrong there.

您需要做的:你有两个选择。第一个是让WCF处理JSONP填充你。您的操作需要有一个属性状态返回的数据类型,只是返回。你也想​​需要启用 CrossDomainScriptAccessEnabled 对您的端点使用的WebHttpBinding财产。你的操作就会看起来像下面的代码:

What you need to do: you have two options. The first one is to let WCF handle the JSONP padding for you. Your operation would need to return a data type with a property "Status", and just return it. You'd also need to enable the CrossDomainScriptAccessEnabled property on the WebHttpBinding used by your endpoint. Your operation would look something like the code below:

public class MyType
{
    public string Status { get; set; }
}

[ServiceContract]
public class Service
{
    [WebGet(UriTemplate = "returndata?s={s}")]
    public MyType ReturnData(string s)
    {
        return new MyType { Status = "OK" };
    }
}



第二个选项,如果你想创建JSONP自己编写代码,将采取在URI附加参数为回调函数的名称,然后创建你的反应时使用它。你还就需要它返回一个,让你没有得到响应作为字符串(这可能是你所拥有的现在)。这将是这样的:

The second option, if you want to create the JSONP code yourself, would be to take an additional parameter in the URI for the callback function name, then use it when creating your response. You'd also need to return it as a Stream, so that you don't get the response as a string (which is probably what you have right now). That would look like this:

[ServiceContract]
public class Service
{
    [WebGet(UriTemplate = "ReturnData?s={s}&callback={callbackFunctionName}")]
    public Stream EchoWithGet(string s, string callbackFunctionName)
    {
        string jsCode = callbackFunctionName + "({\"Status\":\"OK\"});";
        WebOperationContext.Current.OutgoingResponse.ContentType = "application/javascript";
        return new MemoryStream(Encoding.UTF8.GetBytes(jsCode));
    }
}

和这个jQuery代码可以用来访问此服务

And this jQuery code can be used to access this service:

    function StackOverflow_11090835_Test() {
        var url = "/StackOverflow_11090835.svc/ReturnData";
        var data = { s: "Hello world" };
        $.ajax({
            type: 'GET',
            url: url,
            data: data,
            dataType: "jsonp",
            success: function (result) {
                $("#result").text(result.Status);
            }
        });
    }

这篇关于返回JSONP以适当的格式WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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