【发布时间】:2014-07-18 05:02:50
【问题描述】:
我在 jQuery 中有这个连接到我的 WebService 的示例代码
发送文本和接收文本。它的作品非常好。
<html lang="en-US">
<head>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />
<meta name="viewport" content="width=device-width" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
//var WS_URI = 'http://10.0.0.5/WS_Catalog_SQL/Service1.asmx/GetItem';
var WS_URI = 'http://10.0.0.5/WS_Catalog/Service1.asmx/GetItem';
var MAKAT ;
function Look() {
MAKAT = document.getElementById("txt").value.toString();
$.ajax({
ServiceCallID: 1,
url: WS_URI,
type: 'POST',
data: '{"Makat": "' + MAKAT + '"}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success:
function (data, textStatus, XMLHttpRequest) {
//document.writeln(data.d);
$("#RES").text((data.d).toString());
},
error:
function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
}
</script>
</head>
<body >
<br />
<button onclick="Look();">Press me</button>
<input id="txt" value="1111"/>
<br />
<p id="RES">res</p>
</body>
</html>
我的网络服务代码:
[WebMethod]
public DataSet GetItem(string Makat)
{
dsView = new DataSet();
OpenConnection();
SQL = "select Item,Des from MyTbl where Item = '" + Makat + "'";
dsView = new DataSet();
adp_SQL = new SqlDataAdapter(SQL, Conn_SQL);
adp_SQL.Fill(dsView, "MyTbl ");
adp_SQL.Dispose();
if (dsView.Tables["MyTbl "].Rows.Count >= 1)
{
return dsView;
}
else
{
return null;
}
}
现在我需要接收 DataSet (xml),
我构建了接收文本和发送数据集的 WebService(在 C# 中)
但是 jQuery 没有用。
我试试这个:
contentType: "text/xml",
dataType: "text",
得到了这个错误:
POST http://10.0.0.5/WS_Catalog_SQL/Service1.asmx/GetItem 500 (Internal Server Error) jquery.min.js:130
c.extend.ajax jquery.min.js:130
Look (index):14
onclick
我头疼了两天,似乎找不到解决办法....
【问题讨论】:
-
在什么方面不起作用?描述您期望发生的事情和实际发生的事情,以及到目前为止您已经尝试过的事情。
-
我尝试:contentType: "text/xml",dataType: "text", - 但我得到了错误,函数进入错误:section
-
jQuery 运行你的错误回调,因为 500 是一个错误代码。这不是错误,而是一项功能。
-
谢谢,请问如何解决?
-
对于初学者,您可以指定
console.log作为错误回调,以在控制台中打印所有参数,以及使用您的开发工具(例如 Firebug 或 Chrome 开发工具)检查请求。
标签: c# javascript jquery web-services