Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get the JSONP working with WCF Data Services

It seems from all that I read and watched, exposing JSON from a WCF Data Service should be as easy as adding the JSONPSupportBehavior attribute to the service class.

The problem is that VS2010 doesn't recognize the JSONPSupportBehavior attribute.

Is there a reference I am missing? It seems like from all the articles, it was supported out of the box.

like image 918
SevilNatas Avatar asked Jun 06 '10 04:06

SevilNatas


2 Answers

WCF Data Services supports JSON out of the box, no need to add attributes or anything.

In order to receive a response in JSON format clients use standard HTTP content type negotiation.

Specifically, they need to include application/json in the accept header of the request.

There are examples in this section of the OData protocol documentation.

These examples show the raw HTTP requests. Different client HTTP APIs have different mechanisms to specify request headers.

The JSONPSupportBehavior attribute is an example of an extension that allows clients to use a URL query string option (i.e. $format=json) in addition to content-type negotiation, and also adds support for "JSONP" (i.e. $callback=[function-name]).

These are useful in situations where you don't control the headers, such as when doing cross-domain access through script tags.

If you want to use the JSONPSupportBehavior you can obtain it here:

http://code.msdn.microsoft.com/DataServicesJSONP

like image 171
Pablo Castro Avatar answered Nov 13 '22 06:11

Pablo Castro


Newer versions of WCF Data Services support JSON by default and you must have

Accept: application/json;odata=verbose

in the request header.

Accept: application/json

is no longer sufficient. More info here.

like image 2
Nate Cook Avatar answered Nov 13 '22 07:11

Nate Cook