【问题标题】:asp.net 3.5 webservice will not return JSONasp.net 3.5 webservice 不会返回 JSON
【发布时间】:2013-01-29 16:27:48
【问题描述】:

我在网上做了一些搜索,但似乎无法弄清楚。如何强制响应只是 JSON?

web.config

<httpHandlers>
            <remove path="*.asmx" verb="*" />
            <add path="*.asmx" verb="*" validate="false"
                 type="System.Web.Script.Services.ScriptHandlerFactory, 
                 System.Web.Extensions, Version=3.5.0.0,
                 Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

</httpHandlers>

ajax

function get_data() {            
            var objdata = new Object;
            objdata.tool = '1';
            objdata.product = 'Something';
            objdata.details = '9';
            objdata.bogus = 'should not pass validation';
            var server = 'http://crossdomainserver:87';
            var webMethod = '/LogInfo.asmx/LogInfo?';
            var url = server + webMethod + "tool=" + objdata.tool + "&" +
                "product=" + objdata.product + "&" +
                "details=" + objdata.details + "&" +
                "bogus=" + objdata.bogus;
            $('#url').html("<p>" + url + "</p>");

            $.ajax({
                url: url,
                cache: false,
                dataType: "jsonp",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    $('#get').html("<p>" + data + "</p>");
                },
                error: function (e) {

                }
            });


            $('#result').html("<p>" + result.statusText + "</p>");
        }

网络服务

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class LogInfo
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function LogInfo() As String
     'do database/error logic here
     Return jsonreturn.ToString
    End Function

    End Class

回复

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">{"tool":"1","product":"Something","details":"9","user":"username","systime":"1/29/2013 10:20:50 AM","site":"Somewhere, AZ","team":"000000000","result":"1"}</string>

【问题讨论】:

标签: jquery asp.net web-services asmx webmethod


【解决方案1】:

.asmx 无法使用 JSON,您需要使用 WCF 网络服务以 json 格式返回数据。

HTTP Programming with WCF and the .NET Framework 3.5阅读本文

【讨论】:

  • 你的意思是jsonp不能用.asmx吗?
  • 是的,我是这个意思。如果您能给我们提供json.asmx 的样本吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-04
  • 2012-07-02
  • 2012-04-12
  • 1970-01-01
相关资源
最近更新 更多