Enable web API 4.5 to support jquery. getjson (URL, handle) Cross-origin access

Source: Internet
Author: User

Code fragment. After Google for half a day, all the code found was 4.5 RC or earlier versions, some methods have been modified after the official version. I want to modify it and share it with you.

Public static class httprequestmessageextensions {public static string querystring (this httprequestmessage request, string name) {string requesturi = request. requesturi. query; string [] queries = requesturi. split ('&'); foreach (string s in queries) {If (S. contains (name) {int Index = S. indexof ('='); int stopindex = S. indexof ('&', index); If (stopindex! =-1) return S. substring (index + 1, stopindex-index-1); else return S. substring (index + 1, S. length-index-1) ;}} return string. empty ;}} public class jsonpmediatypeformatter: jsonmediatypeformatter {private string callbackqueryparameter; private httprequestmessage httprequest; Public jsonpmediatypeformatter () {supportedmediatypes. add (defamedimediatype); supportedmediatypes. add (New Media Typeheadervalue ("text/JavaScript"); mediatypemappings. Add (New uripathextensionmapping ("jsonp", defaultmediatype);} Public String callbackqueryparameter {get {return callbackqueryparameter ?? "Callback" ;}set {callbackqueryparameter = value ;}} public override mediatypeformatter getperrequestformatterinstance (type, httprequestmessage request, mediatypeheadervalue mediatype) {httprequest = request; return base. getperrequestformatterinstance (type, request, mediatype);} public override task writetostreamasync (type, object value, stream, httpcontent content, transpor Tcontext transportcontext) {string callback; If (isjsonprequest (Out callback) {return task. factory. startnew () => {var writer = new streamwriter (Stream); writer. write (callback + "("); writer. flush (); base. writetostreamasync (type, value, stream, content, transportcontext ). wait (); writer. write (")"); writer. flush () ;}) ;}else {return base. writetostreamasync (type, value, stream, content, transpor Tcontext) ;}} private bool isjsonprequest (out string callback) {callback = NULL; // If (httpcontext. Current. Request. httpmethod! = "Get") if (httprequest. method! = Httpmethod. Get) return false; // var uri = httprequest. requesturi; // var query = URI. query; callback = httprequest. querystring (callbackqueryparameter); return! String. isnullorempty (callback );}}

 
VaR Config = new httpselfhostconfiguration (baseaddress ); // set the max Message Size to 1 m instead of the default size of 64 K and also // set the transfer mode to 'streamed' so that don't allocate a 1 m Buffer but // rather just have a small read buffer. config. maxcompute edmessagesize = 1024*1024; config. transfermode = transfermode. streamed; config. formatters. insert (0, new jsonpmediatypeformatter ());

$ ('# Getjson'). BIND ('click', function () {$. getjson ("http: // 192.168.103.135: 8080/API/products/1? Jsoncallback =? ", Function (data) {$ (" # result "). append (data); return; $ ("# result "). append (data. image); $ (" "). ATTR ("src", "data: image/JPEG; base64," + data. image ). appendto ("# result"); return; $. each (data, function (I, item) {$ ("# result "). append (item. name); $ (" "). ATTR ("src", item. image ). appendto ("# result ");});});});

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.