ajax jquery调用发布的web引用的webservice
写了两个程序A,B,B作为webservice,A添加web引用B,如何ajax,jquery实现对B的接口函数調用,ajax中url怎么设置?望高手帮忙。为这问题困扰我好几天了,真诚希望可以得到有缘人的帮助 ajax webservice jquery web url --------------------编程问答-------------------- 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了! --------------------编程问答-------------------- 你做个测试项目打包传到哪个云盘上我看下吧。 --------------------编程问答-------------------- 楼上的朋友,这样比较麻烦,而且我写的是小团队的,不好公开整个项目的内容。
补充:.NET技术 , ASP.NET