有人可以告诉我如何将 $.ajax() 调用中的变量传递给 ASMX Web 服务吗?
网络服务:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
public class Psyience
{
public int ID;
public string Choice;
public string First;
public string Last;
public string Email;
}
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
[System.Web.Script.Services.GenerateScriptType(typeof(Psyience))]
public class CRUD : System.Web.Services.WebService
{
[WebMethod]
public Psyience[] Anchor()
{
List<Psyience> items = new List<Psyience>();
string constr = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["myConString"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "select Choice from tbl_Psyience WHERE Choice = 'Anchor'";
try
{
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Psyience item = new Psyience();
item.Choice = dr["Choice"].ToString();
items.Add(item);
}
con.Close();
return items.ToArray();
}
catch
{
return null;
}
}
}
这是jQuery:
$(document).ready(function () {
$.ajax({ type: "POST",
url: "Services/CRUD.asmx/Anchor",
data: "{ }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var result = response.d;
var myRecord = "";
$.each(result, function (index, res) {
myRecord = res.Choice;
$('#myDiv').append(myRecord);
});
},
error: function (msg) {
$('#myErr').html("Error while calling web service,,")
}
});
});
我想将一个字符串值从 jQuery 传递到我的 Web 服务中的命令文本。
“从 tbl_Psyience 选择选择 WHERE 选择 = 'passVarToHere'”
不知道如何做到这一点。
提前致谢。