WCF Data Service with OData v3 uses jsonp for cross-origin access, wcfjsonp
WCF Data Service with OData is an excellent Restful Web Service in ASP. NET implementation, but in use, I encountered a problem, that is, when I separately deploy the WDS service, Ajax access requires cross-origin.
In general WCF services, we can use JSONP to solve the problem. So I initiated the following request:
You can see that the response ContentType is application/json, so the browser throws an exception.
Refused to execute script from 'HTTP: // ***. svc/Companies? $ Filter = Type % 20eq % 201 & $... On & jsoncallback = jquery1102003060450653130041_1465881447794 & _ = 1465881447795 'because itsMIME type ('application/json') is not executable, And strict MIME type checking is enabled.
The bold Section tells us,The application/json type is not executable. The return type of JSONP is application/json-p or text/json-p.
However, the response type of the WDS service only supports Atom, XML, and JSON, and does not support JSONP.
Finally, I saw a Blog on MSDN, one of which is:
There are two things needed to support JSONP properly:
- The ability to control the response format. Data Services uses standard HTTP content type negotiation to select what representation of a given resource should be sent to the client (e.g. JSON, Atom). That requires that the caller can set the Accept request header, which is not possible when doing the JSONP trick (which basically just uses <script> tags). We need to add the ability to use the query string in the URL to select format. (e.g. /People(1)/Friends?$orderby=Name&$format=json).
- A new option to wrap the response in a callback if such callback was provided in the request (also in the query string). For example /People(1)/Friends?$orderby=Name&$format=json&$callback=loaded.
That is to say, you only need $ format = json and use $ callback to define the callback function.
Data retrieved successfully .~~