ResponseFormat.Json not responding JSON on production environment
On my localhost i have IIS 7.5 running on windows 7. On my production environment i have iis 7 running on Server Ent. 2008.
When i publish my code to production and run my jQuery ajax call i get an XML response, and i expect a JSON response.
I used fiddler to compare both request headers and they are identical.
EDIT: My service has the [ScriptService]:
[ScriptService]
public class Satellites : System.Web.Services.WebService {
webmethod:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<Satellite> GetSatellites()
{
List<Satellite> satellites;
if (!CacheHelper.Get(strSatelliteKey, out satellites))
{
//satellites not found so retrieve and add to cache for next lookup.
satellites = GetLatestSatellites();
CacheHelper.Add(satellites, strSatelliteKey);
}
return satellites;
}
Ajax call:
$.ajax({
type: "POST",
url: "Satellites.asmx/GetSatellites",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var satellites = response.d;
// console.log(satellites);
$.each(satellites, function (index, satellite) {
Satellites.Items.push(new Satellites.Satellite(satellite.id, satellite.name, satellite.tle0, satellite.tle1, satellite.tle2));
UI.ControlPanel.Satellites.create_li(satellite, index);
});
},
failure: function (msg) {
var message = new UI.ControlPanel.Message("<p>There was an error retrieving source satellite information.</p>", UI.ControlPanel.Message.Type.ERROR);
}
});
Response from Localhost:
{"d":[{,"id":10967,"name":"SEASAT1","tle0":null,"tle1":null,"tle2":null},{,"id":16613,"name":"SPOT1","tle0":null,"tle1":null,"tle2":null},{,"id":20436,"name":"SPOT2","tle0":"SPOT 2","tle1":"1 20436U 90005A 10320.17586019 .00000436 00000-0 83185-4 0 7240","tle2":"2 20436 98.6342 56.4544 0155718 145.6368 215.5023 14.62070104 81087"},{,"id":21574,"name":"ERS1","tle0":null,"tle1":null,"tle2":null},{,"id":21867,"name":"JERS1","tle0":null,"tle1":null,"tle2":null},{,"id":23560,"name":"ERS2","tle0":"ERS-2","tle1":"1 23560U 95021A 10320.16163206 .00000093 00000-0 50826-4 0 7517","tle2":"2 23560 98.5539 32.9598 0001338 91.0148 269.1209 14.32246397814217"},{,"id":23710,"name":"RSAT1","tle0":"RADARSAT-1","tle1":"1 23710U 95059A 10320.17983791 .00000119 00000-0 63183-4 0 8807","tle2":"2 23710 98.5810 324.6746 0001101 92.8062 40.0723 14.29987468784696"},{,"id":25260,"name":"SPOT4","tle0":"SPOT 4","tle1":"1 25260U 98017A 10320.17010216 .00000053 00000-0 45000-4 0 7757","tle2":"2 25260 98.6410 27.5855 0001153 118.8023 241.3191 14.20071681655704"},{,"id":25682,"name":"LSAT7","tle0":"LANDSAT 7","tle1":"1 25682U 99020A 10320.17475749 .00000092 00000-0 30424-4 0 4435","tle2":"2 25682 98.2322 26.4024 0001343 107.7462 252.3889 14.57097903616361"},{,"id":25994,"name":"TERRA1","tle0":"TERRA","tle1":"1 25994U 99068A 10320.19170658 .00000163 00000-0 46333-4 0 3452","tle2":"2 25994 98.2175 32.5202 0000685 91.9171 268.2128 14.57106702580447"},{,"id":27386,"name":"ENVISAT1","tle0":"ENVISAT","tle1":"1 27386U 02009A 10320.19615002 .00000074 00000-0 39337-4 0 6231","tle2":"2 27386 98.5352 25.2483 0001101 100.3084 259.8244 14.37466989455514"},{,"id":27421,"name":"SPOT5","tle0":"SPOT 5","tle1":"1 27421U 02021A 10320.21390906 .00000169 00000-0 10000-3 0 6893","tle2":"2 27421 98.7199 32.2015 0000869 81.7846 312.1186 14.20026544442540"},{,"id":27424,"name":"AQUA","tle0":"AQUA","tle1":"1 27424U 02022A 10319.45619378 .00000236 00000-0 62354-4 0 7940","tle2":"2 27424 98.2036 258.3378 0001264 54.1617 305.9677 14.57123781453943"},{,"id":27838,"name":"ORBVIEW3","tle0":"ORBVIEW 3","tle1":"1 27838U 03030A 10320.08123152 .00007152 00000-0 15929-3 0 425","tle2":"2 27838 96.9884 4.7112 0001609 66.1702 17.5471 15.43897611415052"},{,"id":28931,"name":"ALOS","tle0":"ALOS (DAICHI)","tle1":"1 28931U 06002A 10320.18010103 .00000316 00000-0 76098-4 0 3809","tle2":"2 28931 98.1413 32.2177 0001202 97.6884 262.4473 14.59582841256312"},{,"id":31598,"name":"SKYMED1","tle0":"COSMO-SKYMED 1","tle1":"1 31598U 07023A 10320.12036850 -.00000107 00000-0 -69111-5 0 8384","tle2":"2 31598 97.8612 144.9959 0001295 87.0739 273.0585 14.82168749186198"},{,"id":31698,"name":"TERRASAR-X","tle0":"TERRASAR-X","tle1":"1 31698U 07026A 10320.19654172 .00000567 00000-0 30202-4 0 8531","tle2":"2 31698 97.4447 325.2318 0001498 88.2507 336.5083 15.19155156189768"},{,"id":32376,"name":"SKYMED2","tle0":"COSMO-SKYMED 2","tle1":"1 32376U 07059A 10320.15411372 .00000305 00000-0 44808-4 0 2496","tle2":"2 32376 97.8610 145.0314 0001326 85.1830 274.9536 14.82171023158941"},{,"id":32382,"name":"RSAT2","tle0":"RADARSAT-2","tle1":"1 32382U 07061A 10320.20386927 .00000081 00000-0 48479-4 0 2364","tle2":"2 32382 98.5800 325.5126 0000996 92.7948 331.9613 14.29983070152587"},{,"id":33412,"name":"SKYMED3","tle0":"COSMO-SKYMED 3","tle1":"1 33412U 08054A 10320.14144199 -.00000060 00000-0 -10504-5 0 2737","tle2":"2 33412 97.8612 145.0178 0001265 88.0900 272.0466 14.82171643111404"},{,"id":36605,"name":"TANDEM-X","tle0":null,"tle1":null,"tle2":null}]}
Response from Production:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfSatellite xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://cstars.miami.edu/SAR">
<Satellite>
<id>10967</id>
<name>SEASAT1</name>
</Satellite>
<Satellite>
<id>16613</id>
<name>SPOT1</name>
</Satellite>
<Satellite>
<id>20436</id>
<name>SPOT2</name>
<tle0>SPOT 2</tle0>
<tle1>1 20436U 90005A 10320.17586019 .00000436 00000-0 83185-4 0 7240</tle1>
<tle2>2 20436 98.6342 56.4544 0155718 145.6368 215.5023 14.62070104 81087</tle2>
</Satellite>
<Satellite>
<id>21574</id>
<name>ERS1</name>
</Satellite>
<Satellite>
<id>21867</id>
<name>JERS1</name>
</Satellite>
<Satellite>
<id>23560</id>
<name>ERS2</name>
<tle0>ERS-2</tle0>
<tle1>1 23560U 95021A 10320.16163206 .00000093 00000-0 50826-4 0 7517</tle1>
<tle2>2 23560 98.5539 32.9598 0001338 91.0148 269.1209 14.32246397814217</tle2>
</Satellite>
<Satellite>
<id>23710</id>
<name>RSAT1</name>
<tle0>RADARSAT-1</tle0>
<tle1>1 23710U 95059A 10320.17983791 .00000119 00000-0 63183-4 0 8807</tle1>
<tle2>2 23710 98.5810 324.6746 0001101 92.8062 40.0723 14.29987468784696</tle2>
</Satellite>
<Satellite>
<id>25260</id>
<name>SPOT4</name>
<tle0>SPOT 4</tle0>
<tle1>1 25260U 98017A 10320.17010216 .00000053 00000-0 45000-4 0 7757</tle1>
<tle2>2 25260 98.6410 27.5855 0001153 118.8023 241.3191 14.20071681655704</tle2>
</Satellite>
<Satellite>
<id>25682</id>
<name>LSAT7</name>
<tle0>LANDSAT 7</tle0>
<tle1>1 25682U 99020A 10320.17475749 .00000092 00000-0 30424-4 0 4435</tle1>
<tle2>2 25682 98.2322 26.4024 0001343 107.7462 252.3889 14.57097903616361</tle2>
</Satellite>
<Satellite>
<id>25994</id>
<name>TERRA1</name>
<tle0>TERRA</tle0>
<tle1>1 25994U 99068A 10320.19170658 .00000163 00000-0 46333-4 0 3452</tle1>
<tle2>2 25994 98.2175 32.5202 0000685 91.9171 268.2128 14.57106702580447</tle2>
</Satellite>
<Satellite>
<id>27386</id>
<name>ENVISAT1</name>
<tle0>ENVISAT</tle0>
<tle1>1 27386U 02009A 1032开发者_Python百科0.19615002 .00000074 00000-0 39337-4 0 6231</tle1>
<tle2>2 27386 98.5352 25.2483 0001101 100.3084 259.8244 14.37466989455514</tle2>
</Satellite>
<Satellite>
<id>27421</id>
<name>SPOT5</name>
<tle0>SPOT 5</tle0>
<tle1>1 27421U 02021A 10320.21390906 .00000169 00000-0 10000-3 0 6893</tle1>
<tle2>2 27421 98.7199 32.2015 0000869 81.7846 312.1186 14.20026544442540</tle2>
</Satellite>
<Satellite>
<id>27424</id>
<name>AQUA</name>
<tle0>AQUA</tle0>
<tle1>1 27424U 02022A 10319.45619378 .00000236 00000-0 62354-4 0 7940</tle1>
<tle2>2 27424 98.2036 258.3378 0001264 54.1617 305.9677 14.57123781453943</tle2>
</Satellite>
<Satellite>
<id>27838</id>
<name>ORBVIEW3</name>
<tle0>ORBVIEW 3</tle0>
<tle1>1 27838U 03030A 10320.08123152 .00007152 00000-0 15929-3 0 425</tle1>
<tle2>2 27838 96.9884 4.7112 0001609 66.1702 17.5471 15.43897611415052</tle2>
</Satellite>
<Satellite>
<id>28931</id>
<name>ALOS</name>
<tle0>ALOS (DAICHI)</tle0>
<tle1>1 28931U 06002A 10320.18010103 .00000316 00000-0 76098-4 0 3809</tle1>
<tle2>2 28931 98.1413 32.2177 0001202 97.6884 262.4473 14.59582841256312</tle2>
</Satellite>
<Satellite>
<id>31598</id>
<name>SKYMED1</name>
<tle0>COSMO-SKYMED 1</tle0>
<tle1>1 31598U 07023A 10320.12036850 -.00000107 00000-0 -69111-5 0 8384</tle1>
<tle2>2 31598 97.8612 144.9959 0001295 87.0739 273.0585 14.82168749186198</tle2>
</Satellite>
<Satellite>
<id>31698</id>
<name>TERRASAR-X</name>
<tle0>TERRASAR-X</tle0>
<tle1>1 31698U 07026A 10320.19654172 .00000567 00000-0 30202-4 0 8531</tle1>
<tle2>2 31698 97.4447 325.2318 0001498 88.2507 336.5083 15.19155156189768</tle2>
</Satellite>
<Satellite>
<id>32376</id>
<name>SKYMED2</name>
<tle0>COSMO-SKYMED 2</tle0>
<tle1>1 32376U 07059A 10320.15411372 .00000305 00000-0 44808-4 0 2496</tle1>
<tle2>2 32376 97.8610 145.0314 0001326 85.1830 274.9536 14.82171023158941</tle2>
</Satellite>
<Satellite>
<id>32382</id>
<name>RSAT2</name>
<tle0>RADARSAT-2</tle0>
<tle1>1 32382U 07061A 10320.20386927 .00000081 00000-0 48479-4 0 2364</tle1>
<tle2>2 32382 98.5800 325.5126 0000996 92.7948 331.9613 14.29983070152587</tle2>
</Satellite>
<Satellite>
<id>33412</id>
<name>SKYMED3</name>
<tle0>COSMO-SKYMED 3</tle0>
<tle1>1 33412U 08054A 10320.14144199 -.00000060 00000-0 -10504-5 0 2737</tle1>
<tle2>2 33412 97.8612 145.0178 0001265 88.0900 272.0466 14.82171643111404</tle2>
</Satellite>
<Satellite>
<id>36605</id>
<name>TANDEM-X</name>
</Satellite>
</ArrayOfSatellite>
Even if i use the JavaScriptSerializer and return a string like below i get json wrapped in an xml tag with a <string
> wrapping around the JSON data.
var s = new System.Web.Script.Serialization.JavaScriptSerializer();
string resultJs = s.Serialize(result);
return resultJs;
what gives? Is it IIS 7? EDIT: My web.config in both environments has:
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory" validate="false"/>
</httpHandlers>
Upgrading to Server 2008 R2 and IIS 7.5 did the trick for me.
I can now remove the handlers and it won't make a difference because it's .net 4.0.
The application now outputs JSON as expected. The only thing i did was the upgrade. I did nothing else.
So production environment application uses XML instead of JSON. I would
- Check if both environments have the same .NET framework versions.
Check if Web.config file on production environment has a line with reference to ScriptHandlerFactory like:
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
It sounds like your production site is targeting the older 2.0 ScriptHandlerFactory. Make sure the site is set to a 3.5 or 4.0 site in IIS and double check these configuration steps.
Also, make sure that your web service is decorated with the [ScriptService] attribute, not just the method itself (the [ScriptMethod] attribute there isn't actually necessary).
精彩评论