How to obtain response in Ajax-enabled WCF Service

Source: Internet
Author: User

Recently, I have been studying the Ajax-enabled WCF method to provide services directly to the front-end. I have encountered some minor problems in the optimization of returned data, which are summarized as follows:

Problem 1: If you want the WCF Service method to directly return the stream, but if you directly return the stream, the browser recognizes it as a file stream. A download prompt is displayed;

Question 2: If Gzip is used to compress the JSON data stream, you need to modify the content-encoding attribute in the returned response. Otherwise, the browser will identify the stream as garbled.

 

Solution:WeboperationcontextClass!

In WCF, the request and response objects of the current request cannot be obtained from httpcontext. Current. Therefore, you must useWeboperationcontext(Ps, this class can only get the context in the ASP. NET compatible mode of WCF, the common WCF needs to use operationcontext)

BelowCodeSolve Problem 1 and problem 2 respectively:
Weboperationcontext. Current. outgoingresponse. contenttype ="App/JSON; charset = UTF-8";
Weboperationcontext. Current. outgoingresponse. headers. Add ("Content-encoding","Gzip");

 

 

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.