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.
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: