消费使用jQuery .NET Web服务 [英] consume .net web service using jquery
本文介绍了消费使用jQuery .NET Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我做了一个返回从SQL Server数据库的DataTable这个Web服务。有人能帮助我与jQuery来显示呢?
Web服务
[WebService的空间(namespace =http://tempuri.org/)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)[ScriptService]
公共类的WebService:System.Web.Services.WebService
{
DataTable的DT =新的DataTable(); [的WebMethod]
公开数据表dbAccess的()
{
使用(SqlConnection的康恩=新的SqlConnection(
ConfigurationManager.ConnectionStrings [localConnectionString]
.ConnectionString))
{
使用(SqlDataAdapter的大=新SqlDataAdapter的())
{
conn.Open();
da.SelectCommand =
新的SqlCommand(选择VehicleMake FROM VehicleMakes,康恩);
da.Fill(DT);
}
conn.Close();
}
返回DT;
}
}
这是据我与jQuery的了
<脚本类型=文/ JavaScript的> $(函数(){
$('#Button1的')点击(的getData)。
}); 函数的getData(){
$阿贾克斯({
键入:POST,
网址:WebService.asmx / dbAccess的
数据:{},
的contentType:应用/ JSON的;字符集= UTF-8,
数据类型:JSON
成功:函数(响应){
//善有善报吗?
},
失败:函数(MSG){
//错误信息
}
});
}
< / SCRIPT>
解决方案
在过去,使用与jQuery ASMX服务时,我用类似的职位/ JSON以下内容:
假设我有一个响应类是这样的:
公共ResponseClass
{
公共字符串消息{搞定;组; }
}
和用这样的方法的Web服务:
[的WebMethod]
[ScriptMethod(UseHttpGet =假,ResponseFormat = ResponseFormat.Json)
公共ResponseClass PostResponse()
{
VAR响应=新ResponseClass(){消息=Hello World的};
返回响应;
}
一些HTML是这样的:
< DIV ID =响应>
< / DIV>
JavaScript的:
$。阿贾克斯({
网址:'/MyService.asmx/PostResponse',
数据:{},
键入:POST,
缓存:假的,
数据类型:JSON,
的contentType:应用/ JSON的;字符集= UTF-8,
成功:函数(MSG){
VAR响应= msg.d; //你的响应对象
$('#响应')HTML(response.Message); //响应格内容设置为消息
},
错误:功能(XHR,状态,错误){
警报(错误); //做一些事情,如果有一个错误
}
});
I made this web service that returns a datatable from sql server db. Can someone help me with the jquery to display it?
web service
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class WebService : System.Web.Services.WebService
{
DataTable dt = new DataTable();
[WebMethod]
public DataTable dbAccess()
{
using (SqlConnection conn = new SqlConnection(
ConfigurationManager.ConnectionStrings["localConnectionString"]
.ConnectionString))
{
using (SqlDataAdapter da = new SqlDataAdapter())
{
conn.Open();
da.SelectCommand =
new SqlCommand("SELECT VehicleMake FROM VehicleMakes", conn);
da.Fill(dt);
}
conn.Close();
}
return dt;
}
}
and this is as far as I got with the jquery
<script type="text/javascript">
$(function () {
$('#Button1').click(getData);
});
function getData() {
$.ajax({
type: "POST",
url: "WebService.asmx/dbAccess",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
// What goes here?
},
failure: function (msg) {
//error message
}
});
}
</script>
解决方案
In the past, when using asmx services with jQuery, I used something like the following for post/json:
Assuming that I had a response class like this:
public ResponseClass
{
public string Message { get; set; }
}
And a webservice with a method like this:
[WebMethod]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public ResponseClass PostResponse()
{
var response = new ResponseClass() {Message = "Hello World"};
return response;
}
Some html like this:
<div id="response">
</div>
The javascript:
$.ajax({
url: '/MyService.asmx/PostResponse',
data: "{}",
type: "POST",
cache: false,
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function(msg) {
var response = msg.d; //your response object
$('#response').html(response.Message); //set the response div contents to the Message
},
error: function(xhr, status, error) {
alert(error); //do something if there is an error
}
});
这篇关于消费使用jQuery .NET Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文