ajax jquery调用发布的web引用的webservice 写了两个程序A,B,B作为webservice,A添加web引用B,如何ajax,jquery实现对B的接口函数調用,ajax中url怎么设置?望高手帮忙。为这问题困扰我好几天了,真诚希望可以得到有缘人的帮助ajax webservicejqueryweburl 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 refer: http://www.cnblogs.com/andiki/archive/2010/05/17/1737254.html jquery类似这样:$.ajax({ type: "POST", url: "http://localhost/WebApplication1/WebService1.asmx/HelloWorld", dataType: "xml", success: function (msg) { alert($(msg).text()); }, error: function(e){ alert("failed"); }}); $.ajax的url参数,给webservice的全地址。 <location path="YourWebservice.asmx"> <system.web> <webServices> <protocols> <add name="HttpGet"/> <add name="HttpPost"/> </protocols> </webServices> </system.web></location>参数用 参数名=xxx&参数名=xxx 这种简单的形式post过去就可以了。 function usersubmit() { var userId = $("#userId").val(); //绑定控件的内容 var userPwd = $("#userPwd").val(); var money = $("#moneyMount").val(); if (userId != "" && userPwd != "") //moneyMount一定不为空,通过session传值,否则页面显示不出来 { $.ajax({ async: false, //同步传递 type: "POST", //访问WebService使用Post方式请求 contentType: "application/json;charset=utf-8", //WebService会返回Json类型 url: "http://localhost/BankService.asmx/Pay", //"/Bank/Web/BankService.asmx/Pay", //调用WebService http://localhost:3448/BankService.asmx/Pay data: "{userId:'" + userId + "',userPassword:'" + userPwd + "',userMoney:" + money + "}", //"{userId:'123',userPassword:'123',userMoney:30.0}", // " "{userId:'" + userId + "',userPassword:'" + userPwd + ",'userMoney:" + money + "}", //Email参数 dataType: 'json', // global: false, success: function (result) { //回调函数,result,返回值 var json = eval('(' + result.d + ')'); //string类型转换为JSON对象 // alert(json); var payresult = json.payResult; switch (payresult) { case "Success": alert("支付成功!"); // location.href = "Shopping_Car.aspx"; break; case "NotEnoughMoney": alert("余额不足!请选择货到付款或取消订单"); location.href = "Shopping_Car.aspx"; break; case "UnknowError": alert("未知错误!"); break; case "InvalidAccount": alert("帐号无效或登录帐号密码出错!"); break; default: alert("错误!"); break; } }, error: function () { //location.href = "Shopping_Car.aspx"; alert("失败"); } // Success, //支付成功 NotEnoughMoney,//帐号余额不足 UnknowError,//未知错误 InvalidAccount//帐号无效 }); } else if (userId == "") { alert("用户Id不能为空!"); $("#userId").focus(); } else if (userPwd == "") { alert("用户密码不能为空!"); $("#userPwd").focus(); } }dataType的json换为xml试过了,也没效果。如果不跨应用程序的话,url: "http://localhost:3448/BankService.asmx/Pay", 是成功的,但跨应用程序这段代码就不执行success内函数。好像要要在程序中添加另一个程序的web引用,添加了但不知道url该怎么写。 //moneyMount一定不为空,通过session传值上面这个注释什么意思?两个应用程序之间是不能共享session的。调用WebService要先登录的吗?检查:webserive服务类是否标记为:[System.Web.Script.Services.ScriptService]Pay方法是否标记为:[ScriptMethod(ResponseFormat = ResponseFormat.Json)]Pay方法的参数是否为(string userId, string userPassword, double userMoney)返回的结果类型中是否包含 public string payResult 属性。从本程序和其它程序调用url应该是一样的,都是:http://localhost:3448/BankService.asmx/Pay // 注意只能从本机访问(development server)前台success函数中: var json = eval('(' + result.d + ')'); //string类型转换为JSON对象改成: var json = result.d; [System.Web.Script.Services.ScriptService] public class BankService : System.Web.Services.WebService { [WebMethod] // [ScriptMethod] public string HelloWorld() { return "Hello World"; } //根据客户Id,支付密码,支付金额实现支付功能。 [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string Pay(string userId,string userPassword,decimal userMoney ) { return new Bank.BLL.users().Pay(userId,userPassword,userMoney);//返回支付结果 } }楼上的那个注释没用,第1,2,3点也做到了,第4点我是从本机调用,只是跨应用程序,第5点我调试时success函数都没进去,所以不起作用, var json = eval('(' + result.d + ')');我在单个应用程序中是起作用的,能实现webservice调用。方便的话,加我QQ联系:1394921892 我这里没有你的测试环境,所以很难猜出是什么问题。你可以在浏览器中按F12调试下,看ajax发送出去的request和收到的response是否都正常。webservice也是能调试的,按F5运行,设好断点,收到ajax请求时会停到断点。 楼上的朋友,可否通过QQ上的远程控制给帮我查查问题。我调试了很多次,只知道单步执行到success就跳到error了! “数据安全”和“减少网络通讯”分别是什么意思啊 如何实现ASP.NET+SQl 2000 Server查询功能???? 简简简的小问题 页面刷新后,客户端控件text的值没了 点击登录后弹出几种情况的提示,请问如何做? frame问题,请大家帮忙,急! Access Asp.net 关于部署时候目录的问题~急呀! 文本框不显示小数 用WebClient类实现文件上传的功能的问题 vs调试的时候远程连接服务器出问题了 求助,WWW.XX.COM/(S(r1sozxzdo1vlz4vbhmgbz255))/indexx.aspx中间的那部分怎么去掉
$.ajax({
type: "POST",
url: "http://localhost/WebApplication1/WebService1.asmx/HelloWorld",
dataType: "xml",
success: function (msg) {
alert($(msg).text());
},
error: function(e){
alert("failed");
}
});
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
</location>参数用 参数名=xxx&参数名=xxx 这种简单的形式post过去就可以了。
var userId = $("#userId").val(); //绑定控件的内容
var userPwd = $("#userPwd").val();
var money = $("#moneyMount").val(); if (userId != "" && userPwd != "") //moneyMount一定不为空,通过session传值,否则页面显示不出来
{
$.ajax({
async: false, //同步传递
type: "POST", //访问WebService使用Post方式请求
contentType: "application/json;charset=utf-8", //WebService会返回Json类型
url: "http://localhost/BankService.asmx/Pay", //"/Bank/Web/BankService.asmx/Pay", //调用WebService http://localhost:3448/BankService.asmx/Pay
data: "{userId:'" + userId + "',userPassword:'" + userPwd + "',userMoney:" + money + "}", //"{userId:'123',userPassword:'123',userMoney:30.0}", // " "{userId:'" + userId + "',userPassword:'" + userPwd + ",'userMoney:" + money + "}", //Email参数
dataType: 'json',
// global: false,
success: function (result) { //回调函数,result,返回值
var json = eval('(' + result.d + ')'); //string类型转换为JSON对象
// alert(json);
var payresult = json.payResult;
switch (payresult) {
case "Success":
alert("支付成功!");
// location.href = "Shopping_Car.aspx";
break;
case "NotEnoughMoney":
alert("余额不足!请选择货到付款或取消订单");
location.href = "Shopping_Car.aspx";
break;
case "UnknowError":
alert("未知错误!");
break;
case "InvalidAccount":
alert("帐号无效或登录帐号密码出错!");
break;
default:
alert("错误!");
break;
} },
error: function () {
//location.href = "Shopping_Car.aspx";
alert("失败");
} // Success, //支付成功 NotEnoughMoney,//帐号余额不足 UnknowError,//未知错误 InvalidAccount//帐号无效 });
}
else if (userId == "") {
alert("用户Id不能为空!");
$("#userId").focus();
}
else if (userPwd == "") {
alert("用户密码不能为空!");
$("#userPwd").focus();
}
}dataType的json换为xml试过了,也没效果。如果不跨应用程序的话,url: "http://localhost:3448/BankService.asmx/Pay", 是成功的,但跨应用程序这段代码就不执行success内函数。好像要要在程序中添加另一个程序的web引用,添加了但不知道url该怎么写。
上面这个注释什么意思?两个应用程序之间是不能共享session的。调用WebService要先登录的吗?检查:
webserive服务类是否标记为:[System.Web.Script.Services.ScriptService]
Pay方法是否标记为:[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
Pay方法的参数是否为(string userId, string userPassword, double userMoney)
返回的结果类型中是否包含 public string payResult 属性。
从本程序和其它程序调用url应该是一样的,都是:
http://localhost:3448/BankService.asmx/Pay // 注意只能从本机访问(development server)前台success函数中:
var json = eval('(' + result.d + ')'); //string类型转换为JSON对象
改成:
var json = result.d;
[System.Web.Script.Services.ScriptService]
public class BankService : System.Web.Services.WebService
{ [WebMethod]
// [ScriptMethod]
public string HelloWorld()
{
return "Hello World";
}
//根据客户Id,支付密码,支付金额实现支付功能。
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Pay(string userId,string userPassword,decimal userMoney )
{
return new Bank.BLL.users().Pay(userId,userPassword,userMoney);//返回支付结果
}
}楼上的那个注释没用,第1,2,3点也做到了,第4点我是从本机调用,只是跨应用程序,第5点我调试时success函数都没进去,所以不起作用, var json = eval('(' + result.d + ')');我在单个应用程序中是起作用的,能实现webservice调用。方便的话,加我QQ联系:1394921892
你可以在浏览器中按F12调试下,看ajax发送出去的request和收到的response是否都正常。
webservice也是能调试的,按F5运行,设好断点,收到ajax请求时会停到断点。