0

asp.net で Web サービスを開発しましたが、xml ファイルではなく json 文字列を返すようにしたいと考えています。コードに次のように設定し、インターネット上の複数のソースを調べて試しましたが、「エラー」が表示され続けました。

dao.asmx

 [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string getLosKPIDataYear()
        {
            List<Object> sqlObject = new List<Object>();
            String connectionString = DAO.GetConnectionString();
            string sqlQuery = string.Empty;
            var serializer = new JavaScriptSerializer();
            serializer.MaxJsonLength = Int32.MaxValue;
            SqlConnection sqlConn = new SqlConnection(connectionString);

            try
            {
                sqlQuery = "select LOSYearYear, LOSYearLOS from LOSYear";
                sqlConn.Open();
                SqlDataReader reader = null;
                SqlCommand sqlCommand = new SqlCommand(sqlQuery, sqlConn);
                reader = sqlCommand.ExecuteReader();


                while (reader.Read())
                {
                    sqlObject.Add(new { LOSYearYear = reader["LOSYearYear"].ToString(), LOSYearLOS = reader["LOSYearLOS"].ToString() });

} }

catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            sqlConn.Close();
            var resultData = new { e = sqlObject.ToArray() };
            ContentResult result = new ContentResult();
            result.Content = serializer.Serialize(resultData);
            result.ContentType = "application/json";
            String myResult = result.Content;
            return myResult;
        }

web.config

     <webServices>
          <protocols>
            <add name="HttpGet"/>
            <add name="HttpPost"/>

          </protocols>
        </webServices>

<system.webServer>
    <handlers>
      <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </handlers>
     <modules runAllManagedModulesForAllRequests="true"/>
    <staticContent>
      <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
    </staticContent>
  </system.webServer>

これを解決するにはどうすればよいですか? 前もって感謝します!

4

1 に答える 1

0

これを試して

var JsonString = ....;
$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "YourWebServiceName.asmx/yourmethodname",
    data: "{'TheData':'" + JsonString + "'}",
    dataType: "json",
    success: function (msg) {
        var data = msg.hasOwnProperty("d") ? msg.d : msg;
        OnSucessCallBack(data);
    },
    error: function (xhr, status, error) {
        alert(xhr.statusText);
    }
});

function OnSuccessCallData(DataFromServer) {
 // your handler for success    
}

Web サービス コード:-

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

    [System.Web.Script.Services.ScriptService]
    public class YourWebServiceName : System.Web.Services.WebService
    {
        [WebMethod]
        public string yourmethodname(string TheData)
        {
          JavascriptSerializer YourSerializer = new JavascriptSerializer();
          // custom serializer if you need one 
          YourSerializer.RegisterConverters(new JavascriptConverter  [] { new YourCustomConverter() });

          //deserialization
          TheData.Deserialize(TheData);

          //serialization  
          TheData.Serialize(TheData);
        }
    }
于 2013-04-08T08:40:53.557 に答える