ASP.NET Web服务错误地返回XML而不是JSON [英] ASP.NET web service erroneously returns XML instead of JSON

查看:287
本文介绍了ASP.NET Web服务错误地返回XML而不是JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用jQuery使用ASMX Web服务从JavaScript。当我问了XML,但我想利用.NET的JSON序列化功能正常工作; (它也开​​始来烦我,这是不工作)

I'm attempting to use an ASMX web service from javascript using jQuery. It works fine when I ask for XML, but I want to make use of .net's JSON serialization functionality; (it's also starting to bug me that this isn't working)

在code为Web服务:

The code for the web service:

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;

[WebService()]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class SimpleService : WebService
{
    [WebMethod]
    public String GetString()
    {
        return "value";
    }
}

在code客户端:

The code for the client:

$.ajax({
  type: "POST",
  url: "SimpleService.asmx/GetString",
  data: "{}",
  contentType: "application/json; charset=utf-8",
  dataType: "json"
});

和响应...

Content-Type: text/xml; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET

<?xml version="1.0" encoding="utf-8"?><string xmlns="http://tempuri.org/">value</string>

请求总​​是成功,但jQuery的给了我一个解析器错误(这并不奇怪,因为响应)。

The request always succeeds, but jQuery gives me a parser error (not surprisingly, given the response).

我在我束手无策。我试着将设置为JSON的一个responseTyp的属性ServiceMethod,但似乎没有任何工作。

I'm at my wits end. I've tried adding a ServiceMethod attribute with the ResponseType set to JSON, but nothing seems to work.

我不希望使用.NET的ScriptManager JavaScript的发电机要么,所以请不要使用它们建议。

I don't want to use the .NET ScriptManager javascript generator either, so please do not suggest using them.

推荐答案

这一个是用户错误。

我只是偶然发现了这个计算器的其他问题:<一href=\"http://stackoverflow.com/questions/2749954/asmx-web-service-returning-xml-instead-of-json-in-net-4-0\">web-service净4-0 返回XML而不是JSON的

I just stumbled upon this other stackoverflow question: web-service returning xml instead of json in net 4-0

一个类似的解决方案竟然是我需要的。 web.config文件中曾经为ScriptHandlerFactory的IIS6一个HttpHandler映射,我是用IIS7。添加的HttpHandler映射到web.config的IIS7部分解决了这个问题。

A similar solution turned out to be what I needed. The web.config file had an httpHandler mapping for the ScriptHandlerFactory for IIS6, and I was using IIS7. Adding the httpHandler mapping to the IIS7 section of the web.config solved the problem.

我不喜欢隐藏的移动部件....

I hate hidden moving parts....

这篇关于ASP.NET Web服务错误地返回XML而不是JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆