0

I've done some searching online and can't seem to figure this out. How do I force the response to be just 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>");
        }

webservice

' 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

response

<?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>
John Saunders
  • 160,644
  • 26
  • 247
  • 397
atrueresistance
  • 1,358
  • 5
  • 26
  • 48

1 Answers1

0

JSON is not possible with .asmx you need to use WCF web service to return the data in json format.

Read this for HTTP Programming with WCF and the .NET Framework 3.5

Waqas Raja
  • 10,802
  • 4
  • 33
  • 38