0

I am trying to get the JSON value from the RESTful service.

My service:

namespace WcfService1
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/GetUserDetails")]
        string GetUserDetails();   
    }   
}

I am getting the values from the service in the browser.

enter image description here

But when I am accessing it by the jQuery in my HTML page. It shows the error.

<script type="text/javascript">
    $(document).ready(function () {
        $('#BtnGetData').click(function () {
            debugger;
            $.ajax({
                type: "GET",
                contentType: "application/json; charset=utf-8",
                url: 'http://localhost:22727/Service1.svc/GetUserDetails',
                data: "{}",
                dataType: "json",
                success: function (data) {
                    $($.parseJSON(data.d)).each(function (index, value) {
                        $("#tbDetails").append("<tr><td>" + value.Name + "</td><td>" + value.Email + "</td><td>" + value.Category + "</td><td>" + value.Mobile + "</td><td>" + value.Message + "</td></tr>");
                    });
                },
                error: function (result) {
                    alert(result);
                }
            });
        });
    });
</script>

My Service Method:

public string GetUserDetails(string UserName, string Password)
    {
        if (UserName == "Admin" && Password == "123")
        {
            string file = AppDomain.CurrentDomain.BaseDirectory + "\\DataFile.xml";
            DataSet ds = new DataSet();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row;
            ds.ReadXml(file);
            foreach (DataRow item in ds.Tables[0].Rows)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in ds.Tables[0].Columns)
                {
                    row.Add(col.ColumnName, item[col]);
                }
                rows.Add(row);
            }
            return serializer.Serialize(rows);
        }
        else
        {
            return "null";
        }
    }

It shows the following error in the console:

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Nimit Joshi
  • 1,026
  • 3
  • 19
  • 46
  • run the service always when you are the javascript ajax call – KarthikManoharan Jun 13 '14 at 07:42
  • It is running but i am unable to get the values. @KarthikManoharan – Nimit Joshi Jun 13 '14 at 07:45
  • check out this solution http://supportforums.blackberry.com/t5/Web-and-WebWorks-Development/WCF-webservice-calling-from-Jquery-ajax/m-p/2716149#M42305 – Rachit Patel Jun 13 '14 at 10:33
  • http://stackoverflow.com/questions/14051432/the-server-responded-with-a-status-of-405-method-not-allowed – Rachit Patel Jun 13 '14 at 10:33
  • Can you rather return an object and leave the serialization to JSON format to be handled by the framework. When you specify the contenttype the framework would automatically convert the object to the appropriate format (either XML or JSON) – Rajesh Jun 13 '14 at 12:32

1 Answers1

0

i think json is not in correct format it should like

 "[{"Name" :"Nimit","Email:test@test.com"... 

like this and i have used your ajax call for my service it's working fine.so the error is in service json is not in format.

KarthikManoharan
  • 805
  • 5
  • 12