ASP.Net web service won't return JSON - Always XML

╄→гoц情女王★ 提交于 2020-01-23 13:22:32

问题


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);
        }
    });

});

回答1:


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>


来源:https://stackoverflow.com/questions/9469189/asp-net-web-service-wont-return-json-always-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!