C# 通过Ajax和JSON调用WCF REST服务
我在WCF(demo)中创建了一个Rest服务,它的输出为:C# 通过Ajax和JSON调用WCF REST服务,c#,javascript,ajax,json,wcf,C#,Javascript,Ajax,Json,Wcf,我在WCF(demo)中创建了一个Rest服务,它的输出为:{“GetEmployeeJSONResult”:{“Id”:101,“Name”:“Sumanth”,“Salary”:5000} 现在我已经在asp.net中创建了一个网站,通过AJAX JSON调用这个rest服务 我的代码如下: <script type="text/javascript"> $(document).ready(function () { // $.get
{“GetEmployeeJSONResult”:{“Id”:101,“Name”:“Sumanth”,“Salary”:5000}
现在我已经在asp.net中创建了一个网站,通过AJAX JSON调用这个rest服务
我的代码如下:
<script type="text/javascript">
$(document).ready(function () {
// $.getJSON("http://localhost/SampleService/Service1.svc/getJson?callback=?", null, function (data) {
// alert(data.Name);
// });
var endpointAddress = "http://localhost/SampleService/Service1.svc";
var url = endpointAddress + "/GetJson";
$.ajax({
type: 'GET',
url: url,
dataType: 'jsonp',
contentType: 'application/json',
data: "{}",
success: function (result) {
//alert(JSON.stringify(result));
alert(result.length);
},
error:function(jqXHR)
{
alert(jqXHR.status);
}
});
});
</script>
namespace WcfServiceXmlAndJsonDemo
{
[ServiceContract]
public interface IService1
{
#region OperationContracts
[OperationContract]
[WebInvoke(Method="GET",UriTemplate="GetXml",ResponseFormat=WebMessageFormat.Xml,RequestFormat=WebMessageFormat.Xml,BodyStyle=WebMessageBodyStyle.Bare)]
EmployeeXML GetEmployeeXML();
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetJson", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
List<EmployeeJSON> GetEmployeeJSON();
#endregion
}
}
DataContract EmployeeJSON:
namespace WcfServiceXmlAndJsonDemo
{
[DataContract]
public class EmployeeJSON
{
#region Properties
[DataMember]
public string Name { get; set; }
[DataMember]
public int Id { get; set; }
[DataMember]
public double Salary { get; set; }
#endregion
}
}
Service1.svc.cs:
namespace WcfServiceXmlAndJsonDemo
{
public class Service1 : IService1
{
public List<EmployeeJSON> GetEmployeeJSON()
{
EmployeeJSON json = new EmployeeJSON()
{Name="Sumanth",Id=101,Salary=5000.00 };
return json;
}
}
}
名称空间WcfServiceXmlAndJsonDemo
{
公共类服务1:IService1
{
公共列表GetEmployeeJSON()
{
EmployeeJSON=newEmployeeJSON()
{Name=“Sumanth”,Id=101,工资=5000.00};
返回json;
}
}
}
请帮我解决这个问题
先谢谢你
Krunal使用
WebGet
更改WebInvoke
并删除Method=“GET”
在AJAX调用中
type: 'GET',
url: url,
dataType: 'jsonp',
//contentType: 'application/json', No need to mention Content-Type.
试试看。此功能应该可以:
function () {
$.ajax({
type: "GET",
async: false,
contentType: "application/json; charset=utf-8",
url: 'http:www.svc/Service1.svc/GetJson',
data: "{ }",
processData: false,
dataType: "json",
success: function (data) {
var result = data.GetEmployeeJSONResult;
var id = result.Id;
var name = result.Name;
var salary = result.Salary;
$('#jsonData').html('');
$('#jsonData').append('<table border="1"><tr><th>Employee Id</th><th>Name</th><th>Salary</th></tr><tr><td>' + id + '</td><td>' + name + '</td><td>' + salary + '</td></tr></table>');
},
error: function (xhr) {
alert(xhr.responseText);
}
});
}
函数(){
$.ajax({
键入:“获取”,
async:false,
contentType:“应用程序/json;字符集=utf-8”,
url:'http:www.svc/Service1.svc/GetJson',
数据:“{}”,
processData:false,
数据类型:“json”,
成功:函数(数据){
var result=data.GetEmployeeJSONResult;
var id=result.id;
var name=result.name;
var工资=结果工资;
$('#jsonData').html('');
$('#jsonData').append('Employee-IdNameSalary'+id+''+name+''+salary+'');
},
错误:函数(xhr){
警报(xhr.responseText);
}
});
}
您应该尝试从fiddler或任何请求跟踪工具获取请求,以确保服务正常工作,然后继续使用Ajax。如果您在fiddler中遇到任何错误,请在此提及。