WCF Data Service with OData v3 uses jsonp for cross-origin access, wcfjsonp

Source: Internet
Author: User

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 .~~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.