Web服务从Web方法获取字符串数据时出现问题 [英] Web Service Problem in getting string data from web method

查看:55
本文介绍了Web服务从Web方法获取字符串数据时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我已经创建了一个Web服务,我想从ajax帖子发送ID并想要获取
名称作为Web服务的响应.但是我出错了.当debugg发出成功命令时

Hi!

I have created a web service, I want to send id from ajax post and want to get the
name as response from web service. But I am getting error. when debugg goes on success command

 <script type="text/javascript">
    function callme()
    {
    var postData ={'id':'4'};
    var pdataJSON=JSON.stringify(postData);
          $.ajax({
            type: 'POST',
            contentType: 'application/json',
            url: '/JSON/WebService.asmx/GetName',
            dataType: "json",
            data:pdataJSON,
            success: function(responseText){
                alert(data.responseText);
            },
            error: function(jqXHR, textStatus, errorThrown){
                alert(jqXHR, textStatus, errorThrown);
            }
        });
}
    </script>


以下是网络服务


Following is a web service

[WebMethod]
   public string GetName(Int32 id)
   {
       var result = from p in obj.tblPersons
                    where p.id == id
                    select p;
       return result.First().name;

   }



请帮助我



please help me

推荐答案

.ajax({ 类型:' POST', contentType:' application/json', 网址:' /JSON/WebService.asmx/GetName', dataType:" , 数据:pdataJSON, 成功:功能(responseText){ alert(data.responseText); }, 错误:函数(jqXHR,textStatus,errorThrown){ alert(jqXHR,textStatus,errorThrown); } }); } </script>
.ajax({ type: 'POST', contentType: 'application/json', url: '/JSON/WebService.asmx/GetName', dataType: "json", data:pdataJSON, success: function(responseText){ alert(data.responseText); }, error: function(jqXHR, textStatus, errorThrown){ alert(jqXHR, textStatus, errorThrown); } }); } </script>


以下是网络服务


Following is a web service

[WebMethod]
   public string GetName(Int32 id)
   {
       var result = from p in obj.tblPersons
                    where p.id == id
                    select p;
       return result.First().name;

   }



请帮助我



please help me


webmethod接收参数的类型必须是object.因为您要从脚本发送的json对象不是整数.
The webmethod receiving parameter type must be object. because you are sending json object from script not an integer.
[WebMethod]
   public string GetName(Object id)
   {
       var result = from p in obj.tblPersons
                    where p.id == id
                    select p;
       return result.First().name;

   }


请检查一次您的代码.


please check your code once. it may helps you.


选中此链接,可能会对您有帮助.

将JSON对象传递给WCF服务 [
Check this link, it may helps you.

pass JSON object to WCF service[^]


这篇关于Web服务从Web方法获取字符串数据时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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