using System.Collections.Generic; using System.Web.Http; using ExtJS.WebApi.Data; namespace extjs.webapi.controllers{ [Routeprefix ("Api/book")] public class Bookcontroller:apicontroller { [Route ("Get")] public list<bookmodel> Get () { return bookrepository.list;}} }
Using Firefox, show:
The returned "Content-type" is "Application/xml"
ExtJS 4
Ext.regmodel (' BookInfo ', { ' Name ''string ' }] }); var bookstore = ext.create (' Ext.data.JsonStore ', { ' BookInfo ', proxy: { ' ajax ', ' Book/get ', ' JSON ' }}} );
...
{xtype: ' combo ' , Fieldlabel: book List ' , Listconfig: {loadingtext: ' loading book information ' , empt Ytext: }, TriggerAction: ' all ' , Store:bookstore, Displayfield: "Name" , Valuefield: "N Ame ", Querymode: ' remote ' , querydelay:300}
The request succeeds, and the return data is XML
The data cannot be displayed in the drop-down box
IE no problem. The requested data is data in Application/json format
But in IE, the return is "Application/json"
In order to solve Firefox, request ASP. NET WEBAPI data, get the data of XML, need to do this:
public class browserjsonformatter: Jsonmediatypeformatter {public browserjsonformatter () {this . Supportedmediatypes.add (new mediatypeheadervalue ( "text/html" )); this . serializersettings.formatting = formatting.indented; } public override void Setdefaultcon Tentheaders (type type, httpcontentheaders headers, Mediatypeheadervalue mediatype) { Base . Setdefaultcontentheaders (type, headers, mediatype); Headers. ContentType = new mediatypeheadervalue ( "Application/json" ) { CharSet = "UTF-8" }; } }
\app_start\webapiconfig.cs
Public Static void Register (httpconfiguration config) { ... Config. Formatters.add (new browserjsonformatter ()); }
Request the API again
Reference:
Http://stackoverflow.com/questions/9847564/how-do-i-get-asp-net-web-api-to-return-json-instead-of-xml-using-chrome
ASP. NET Web Api Returns the result format, and the display in the browser