Showing posts with label JSON. Show all posts
Showing posts with label JSON. Show all posts

Monday, May 27, 2013

Jquery Ajax post Custom Object as Json to Wcf Service


function LoadMore() {

    var myData =
    {
        arguments : {
            Category: "Main",
            SearchText: "",
        }
    };


    var jsonedArguments = JSON.stringify(myData);

    $.ajax({
        type: "POST",
        url: "/PublicService.svc/LoadMore",
        contentType: "application/json; charset=utf-8",
        cache: false,
        data: jsonedArguments,
        dataType: "json",
        processdata: true,
        success: function (data) {
         
            debugger;
            var xx = data;

        },
        error: function (msg) {
            alert('CallFiles failed!!!!');
        }
    });
}



 [OperationContract]
        [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        public GetFilesResult LoadMore(GetFilesArguments arguments)
        {
            return new GetFilesResult() { TotalCount = 100 };
        }

Thursday, July 5, 2012

WCF Json Service Method Argument null Problem

Do NOT use "url" as a argument name.

Wcf Service Method

 [OperationContract]
 [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json,
         RequestFormat= WebMessageFormat.Json)]
PageDTO PageByUrl(String url,String language);  --> PageDTO PageByUrl(String title,String language); 


-----------------------------------------------------------------------

Requested Url:

http://localhost:2222/tr-TR/test-page-1
-----------------------------------------------------------------------


Jquery Ajax Call


<script type="text/javascript">

    $(document).ready(function () {
     
        data = window.location.pathname.split("/");
        urlStr = data[data.length -1];
        languateStr = data[data.length - 2];

        var data = new Object();
        data.url = urlStr;   ---> data.title=urlStr;
        data.language = languateStr;

        //var xxx = $.toJSON(data);
        //result: {"url":" test-page-1","language":"tr-TR"}

        caller_dynamic(data);
    });


    var menuUrl = "http://localhost:9000/PublicService.svc/PageByUrl";

    function caller_dynamic(mydata) {
        $.support.cors = true;
        $.ajax({
            type: "POST",
            url: menuUrl,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: $.toJSON(mydata),
            success: function (data) {
                $("#content").html(data.Content);
            },
            error: function (xhr) {
                alert(xhr.responseText);
            }

        });
    }
     < /script>

---------------------------------------------------------------------

public PageDTO PageByUrl(String url, String language) --> (String title, String language)
        {
            try
            {
                var publicBS = unityContainer.Resolve();
                var page = publicBS.PageByUrl(title);
                return page;
            }
            catch (Exception ex)
            {
                throw DynFaultException.Instance.CreateFault(ex);
            }
        }



Conclusion

if url is null, do NOT panic. just change the name of the argument name url to title.





Monday, July 2, 2012

WCF Method not Allowed

Check type of the method call.

GET - It must be uppercase

        [OperationContract]
        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
        String Test();
     
$.ajax({
                     cache: false,
                     type: "GET",
                     async: false,
                     dataType: "json",
                     url: "PublicService.svc/Test",
                     success: function (data) {
                         debugger;
                         $("#containerDiv").html(data);
                     },
                     error: function (xhr) {
                         debugger;
                         alert(xhr.responseText);
                     }

                 });


POST - It must be uppercase



  [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
        String Test2();


$.ajax({
                     cache: false,
                     type: "POST",
                     async: false,
                     dataType: "json",
                     url: "PublicService.svc/Test2",
                     success: function (data) {
                         debugger;
                         $("#containerDiv").html(data);
                     },
                     error: function (xhr) {
                         debugger;
                         alert(xhr.responseText);
                     }

                 });



Thursday, June 21, 2012

ASP.NET Configure Json Serialization MaxLenght recursionLimit

Schema

<jsonSerialization 
      maxJsonLength="number" 
      recursionLimit="number">
    <converters>... </converters>
</jsonSerialization>

Defaults

The default length is 102400
The default recursion limit is 100

Sample

<system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="500000">
        </jsonSerialization>
      </webServices>
    </scripting>
  </system.web.extensions>

Short Sample

<jsonSerialization maxJsonLength="50" recursionLimit="10" />

Resource:

http://msdn.microsoft.com/en-us/library/bb513882.aspx