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[^]