ASP.Net Web服务将不会返回JSON - 始终XML [英] ASP.Net web service won't return JSON - Always XML

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

问题描述

我想创建一个简单的AJAX和放大器; Web服务测试(使用C#.NET 2.0)返回JSON格式的数据,我有(我认为)我需要的一切,但我一直运行到同样的问题,一遍又一遍。从web服务的响应总是XML。 (它总是有< XML版本=1.0编码=UTF-8> 作为1号线)。

我已经试过来的ScriptMethod标记中的各种选择,但没有什么差别。

调用工作好,但我得到一个parsererror的回应,我希望,因为它不是有效的JSON。我可以调用浏览器中的Web服务方法,并得到的返回值我想到,就在XML格式。菲德勒也说明了请求和响应的,因为我已经找到了。

有已经在这里的确切同一问题的几个问题,但他们都没有给我一个工作的答案。

请提问,如果你需要更多的信息。

C#

 使用系统;
使用System.Collections.Generic;
使用的System.Web;
使用System.Web.Services;
使用System.Web.Script.Services;
使用System.Web.Script.Serialization;

命名空间jQueryWebServiceTest
{
    ///<总结>
    ///这是一个简单的Web服务来测试使用AJAX从JavaScript调用,并得到响应。
    ///< /总结>
    [WebService的(命名空间=htt​​p://tempuri.org/)]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)
    [System.ComponentModel.ToolboxItem(假)]
    [ScriptService]
    公共类服务1:System.Web.Services.WebService
    {
        [WebMethod的]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)
        公共字符串的SayHello()
        {
            //我都试过这个...
            VAR响应=新{值=你好};
            JavaScriptSerializer JSON =新JavaScriptSerializer();
            返回json.Serialize(响应);

            //  和这个...
            返回你好;
        }
    }
}
 

Javascript的

  $(函数(){

    VAR数据= JSON.stringify({名称:约翰}); //这是不使用 - 其它测试的一部分。
    数据={};

    $阿贾克斯({
        键入:POST,
        数据:数据,
        网址:TestService.asmx / SayHello的,
        的contentType:应用/ JSON的;字符集= UTF-8,
        数据类型:JSON,
        成功:功能(数据){
            的console.log(数据);
        },
        错误:功能(错误,状态){
            的console.log(状态=+状态);
            执行console.log(错误);
        }
    });

});
 

解决方案

我设法得到它的工作,加入了以下2个街区到web.config ...

在里面的System.Web ...

配置

 <组件>
    <添加组件=System.Web.Extensions程序,版本= 1.0.61025.0,文化=中性公钥= 31BF3856AD364E35/>
< /组件>
 

和其他地方,里面的的System.Web ...

 < HttpHandlers的>
    <删除动词=*路径=* ASMX。/>
    <加上动词=*路径=*。ASMXTYPE =System.Web.Script.Services.ScriptHandlerFactory验证=FALSE/>
< / HttpHandlers的>
 

I'm trying to create a simple AJAX & web service test (using C# .Net 2.0) to return data in JSON format, and I have (I believe) everything I need but I keep running into the same problem over and over again. The response from the web service is always XML. (It always has <?xml version="1.0" encoding="utf-8"?> as the 1st line).

I've tried the various options that come within the ScriptMethod tag, but nothing makes any difference.

The call works okay, but I get a "parsererror" with the response, which I would expect as it's not valid JSON. I can call the web service method within the browser and get the return value I expect, just in XML format. Fiddler also shows that the request and response are as I've found.

There are already several questions on here about the exact same issue, but none of them have given me a working answer.

Please ask questions if you need more information.

C#

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;

namespace jQueryWebServiceTest
{
    /// <summary>
    /// This is a simple web service to test calling from javascript using AJAX, and getting a response.
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string SayHello()
        {
            //  I've tried both this...
            var response = new { value = "Hello" };
            JavaScriptSerializer json = new JavaScriptSerializer();
            return json.Serialize(response);

            //  and this...
            return "Hello";
        }
    }
}

Javascript

$(function () {

    var data = JSON.stringify({ Name: "John" });  // This is not used - part of other testing.
    data = "{}";

    $.ajax({
        type: "POST",
        data: data,
        url: "TestService.asmx/SayHello",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            console.log(data);
        },
        error: function (error, status) {
            console.log("status = " + status);
            console.log(error);
        }
    });

});

解决方案

I managed to get it working by adding the following 2 blocks to the web.config...

In the configuration block inside system.web...

<assemblies>
    <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>

and anywhere else, inside system.web...

<httpHandlers>
    <remove verb="*" path="*.asmx"/>
    <add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory" validate="false"/>
</httpHandlers>

这篇关于ASP.Net Web服务将不会返回JSON - 始终XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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