0

ASP.NET Web フォーム プロジェクトに Web サービスがあり、データを Web サービスのメソッドに POST してから、既定の XML ではなく JSON オブジェクトを返そうとしています。

例のために単純化された私の Web サービス メソッドは次のようになります。

    [WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public DataControl.JSONMessage NavigationItemsUpdate(string description, int id, string linkText)
    {

            DataControl.JSONMessage response = new DataControl.JSONMessage();
            response.Name = "Item Update";
            return response;
        }
    }  

AJAX 経由で次の POST を送信する

 linkText=Skill+AquisitionAaAa&sequence=5&description=Learn+new+game+skills&uid=7&oper=edit&id=7

応答が XML であることを除いて、既定のコンテンツ タイプで動作します。

 Content-Type   application/x-www-form-urlencoded; charset=UTF-8

応答:

 <?xml version="1.0" encoding="utf-8"?>
 <JSONMessage xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/">
  <Name>Item Update</Name>
  <State>Failed</State>
  <Message0>Update</Message0>
 </JSONMessage>

JSON 応答を取得できた唯一の方法は、AJAX 呼び出しのコンテンツ タイプを

  contentType: "application/json",

Asp.Net の Web サービスから JSON 応答を取得しますが、POST パラメータの読み取りに問題があり、この例外を返します

 {"Message":"Invalid JSON primitive: linkText.","StackTrace":"   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}

POST が通常の POST ではなく JSON であると想定されるため

私の問題は、Web サービス メソッドに POST パラメータを期待させながら JSON で応答させるにはどうすればよいかということです。

4

0 に答える 0