在我的本地主机上,我在Windows 7上运行IIS 7.5。在我的生产环境中,我在服务器实体上运行了iis 7。 2008。
当我将我的代码发布到生产并运行我的jQuery ajax调用时,我得到一个XML响应,我期待一个JSON响应。
我使用fiddler来比较两个请求标头,它们是相同的。
编辑:我的服务有[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电话:
$.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);
}
});
来自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}]}
生产的回应:
<?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 10320.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>
即使我使用JavaScriptSerializer并返回如下所示的字符串,我也会将json包装在带有<string
&gt;的xml标记中。包装JSON数据。
var s = new System.Web.Script.Serialization.JavaScriptSerializer();
string resultJs = s.Serialize(result);
return resultJs;
是什么给出的?是IIS 7吗? 修改 我在两种环境中的web.config都有:
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory" validate="false"/>
</httpHandlers>
答案 0 :(得分:1)
升级到Server 2008 R2 和 IIS 7.5 为我做了诀窍。
我现在可以删除处理程序,它不会有所作为,因为它是.net 4.0。
应用程序现在按预期输出JSON。我唯一做的就是升级。我什么也没做。
答案 1 :(得分:0)
因此生产环境应用程序使用XML而不是JSON。我会
检查生产环境中的Web.config文件是否有一行引用 ScriptHandlerFactory ,如:
<add verb="*" path="*.asmx" validate="false"
type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
答案 2 :(得分:0)
听起来您的制作网站的目标是旧的2.0 ScriptHandlerFactory。确保该站点在IIS和double check these configuration steps中设置为3.5或4.0站点。
另外,请确保您的Web服务使用[ScriptService]属性进行修饰,而不仅仅是方法本身(实际上没有必要的[ScriptMethod]属性)。