从我的 jquery 调用 webserivce
我创建了这样的代码来访问我的网络服务中的 AddNums 方法。我通过网络服务发送数据以获取输出。但它没有给出任何输出。
<html xmlns="http://www.w3.org/1999/xhtml">
<script src="scripts/Jquery%20v1.6.4.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#btn").click(function () {
alert('I have been clicked');
$.ajax({
type: "POST",
url: "http://localhost:5554/Service1.svc",
data: "{2,3}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$("#output").text(msg.d);
}
});
});
});
</script>
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<input type="button" id="btn" value="Click Me" /> <br /> <br />
<span id="output"></span>
</form>
</body>
</html>
实施网络服务。我已经在 Visual Studio 中使用内置客户端测试了 Web 服务,它运行良好。
namespace WcfServiceTest
{
[System.Web.Script.Services.ScriptService]
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
[System.Web.Services.WebMethod(BufferResponse = false)]
public int AddNums(int val1, int val2)
{
return (val1 + val2);
}
}
}
I have created a code like this to access method AddNums in my webservice. I am sending data through webservice to get output. but it is not giving any output.
<html xmlns="http://www.w3.org/1999/xhtml">
<script src="scripts/Jquery%20v1.6.4.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#btn").click(function () {
alert('I have been clicked');
$.ajax({
type: "POST",
url: "http://localhost:5554/Service1.svc",
data: "{2,3}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$("#output").text(msg.d);
}
});
});
});
</script>
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<input type="button" id="btn" value="Click Me" /> <br /> <br />
<span id="output"></span>
</form>
</body>
</html>
Implementation of webserivce. I have tested webservice with inbuilt client in Visual Studio, it is working perfectly.
namespace WcfServiceTest
{
[System.Web.Script.Services.ScriptService]
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
[System.Web.Services.WebMethod(BufferResponse = false)]
public int AddNums(int val1, int val2)
{
return (val1 + val2);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是错误的方法,
正确的方法是
将参数传递给外部文件。
This is wrong way
Right Way is
for passing the parameter to external file.