通过Ajax JSON调用WCF Rest服务 [英] Calling WCF Rest service through Ajax JSON

查看:96
本文介绍了通过Ajax JSON调用WCF Rest服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在WCF(演示)中提供了一个Rest服务,它输出为:{GetEmployeeJSONResult:{Id: 101,姓名:Sumanth,薪水:5000}}



现在我在asp.net创建了一个网站,我称之为休息通过AJAX JSON服务...



我的代码如下:

< 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);
},
错误:function(jqXHR)
{
alert(jqXHR.status);
}
});

});
< / script>



您可以看到我通过AJAX和getJSON方法访问过服务..



现在,当我发出数据警报时,它显示输出为未定义..

i尝试过:

alert(result.d.length),警报(结果) .d.GetEmployeeJSONResult)...但是总是把我看作未定义..两种方法..



请帮我解决这个问题..





提前感谢您。



Krunal

解决方案

(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({
类型:'GET',
url:url,
dataType:'jsonp',
contentType:'application / json',
数据:{},
成功:函数(结果){
//alert(JSON.stringify(result));
alert(result.length);
},
错误:函数(jqXHR)
{
alert(jqXHR.status);
}
});

});
< / script>



您可以看到我通过AJAX和getJSON方法访问过服务..



现在,当我发出数据警报时,它显示输出为未定义..

i尝试过:

alert(result.d.length),警报(结果) .d.GetEmployeeJSONResult)...但是总是把我看作未定义..两种方法..



请帮我解决这个问题..





提前感谢您。



Krunal

Hello everyone,

I have made one Rest service in WCF (demo) , which gives me output as : {"GetEmployeeJSONResult":{"Id":101,"Name":"Sumanth","Salary":5000}}

Now i have created one website in asp.net in which i am calling this rest service through AJAX JSON...

my code is as below:

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


You could see that i have accessed service through both AJAX and getJSON methods..

now when i am making a alert of data , it shows me output as undefined..
i have tried :
alert(result.d.length) , alert(result.d.GetEmployeeJSONResult)... but always shows me as undefined..in both methods..

Please help me out how to handle this..


Thanking you in advance.

Krunal

解决方案

(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>


You could see that i have accessed service through both AJAX and getJSON methods..

now when i am making a alert of data , it shows me output as undefined..
i have tried :
alert(result.d.length) , alert(result.d.GetEmployeeJSONResult)... but always shows me as undefined..in both methods..

Please help me out how to handle this..


Thanking you in advance.

Krunal


这篇关于通过Ajax JSON调用WCF Rest服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆