62,046
社区成员
发帖
与我相关
我的任务
分享
function ok() {
var mes= $("#txtOpin").val(); ;
$.ajax({
url: "backGroundService.asmx/saveOpinion",
dataType: "text",
data: "mes",
contentType: "application/json; charset=utf-8",
success: function (msg) { //msg就是方法返回值
//这里是请求成功做的操作
alert(msg);
},
error: function (xhr, msg, e) {
//这里是失败了
alert(msg);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace XJNWeb.template
{
/// <summary>
/// backGroundService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class backGroundService : System.Web.Services.WebService
{
public static string saveOpinion(string mes)
{
if (mes == "")
{
return "请填写意见";
}
else
{
return "保存成功";
}
}
var mes= $("#txtOpin").val();
$.get("backGroundService.asmx/saveOpinion", { mes: mes }, function (msg) {
alert($(msg).text());
})
<webServices>
<protocols>
<add name="HttpPost" />
<add name="HttpGet" />
</protocols>
</webServices>
复制我代码。在要用Jquery调用的页面和webService同一级目录。如果没效果我可以去si了、
$("#Button1").click(function () {
$.ajax({
type: "POST",
contentType: "application/json",
url: "../WebService1.asmx/HelloWorld",
data: "{msg:'alpha'}",
dataType: 'json',
success: function (result) {
alert(result.d);
},
error: function (e) { alert('error'); }
});
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebApplication1
{
/// <summary>
/// WebService1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld(string msg)
{
return "Hello World:"+msg;
}
}
}