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 ");});});});