我正在尝试使用带有 C# 和 jquery mobile 的 asp.net 3.5 中的 web 服务从数据库中填充列表。
当我运行该站点时它工作正常,但是当我将它发布到我的服务器时它不会填充列表并且 ajax 错误函数会捕获一个错误。这是我的代码。
我的网络服务:
public class product
{
//[product] ,[img1] ,[descr]
public string name;
public string img1;
public string descr;
}
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class CarService : System.Web.Services.WebService {
[WebMethod]
public List<product> GetAllProducts()
{
List<product> productt = new List<product> {};
string query = "SELECT [product] ,[img1] ,[descr] FROM [ELQ].[dbo].[products]";
SqlCommand cmd = new SqlCommand(query);
DataSet ds = GetData(cmd);
DataTable dt = ds.Tables[0];
// Process each employee record in table...
//foreach (DataRow dr in dt.Rows)
foreach(DataRow item in ds.Tables[0].Rows)
{
//productt.Add((DataRow) row);
product pro = new product();
pro.name = item["product"].ToString();
pro.img1 = item["img1"].ToString();
pro.descr = item["descr"].ToString();
productt.Add(pro);
}
return productt;
}
public static string GetShipDetails()
{
string query = "SELECT [product] ,[img1] ,[descr] FROM [ELQ].[dbo].[products]";
SqlCommand cmd = new SqlCommand(query);
return GetData(cmd).GetXml();
}
private static DataSet GetData(SqlCommand cmd)
{
string connString = "Data Source=GHOST-PC\\STC;Initial Catalog=ELQ;Integrated Security=True";
using (SqlConnection con = new SqlConnection(connString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
}
}
剧本:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="asp" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head runat="server">
<title>jQuery Mobile Market</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>
<script type="text/javascript">
$(document).on('pageinit', function () {
Greating();
});
function Greating() {
$.ajax({
type: "POST",
url: "CarService.asmx/GetAllProducts",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var productt = response.d;
$.each(productt, function (index, product) {
$("#theList").append("<li><img src='pic/" + product.img1 + "'> <br /> " + product.name + " <br />" + product.descr + "</li>");
});
$("#theList").listview("refresh");
},
error: function (response) {
$("#theList").append("<li>error<li>");
}
});
}
</script>
它看起来像 json 响应是错误的并产生错误,但是当我从笔记本电脑执行代码时它工作正常。我不知道我错过了什么,所以请帮助我。
错误:
POST http://192.168.1.7:7777/CarService.asmx/GetAllProducts 500 (Internal Server Error) jquery-1.8.2.min.js:2
send jquery-1.8.2.min.js:2
p.extend.ajax jquery-1.8.2.min.js:2
Greating 192.168.1.7:18
(anonymous function) 192.168.1.7:12
p.event.dispatch jquery-1.8.2.min.js:2
g.handle.h jquery-1.8.2.min.js:2
p.event.trigger jquery-1.8.2.min.js:2
(anonymous function) jquery-1.8.2.min.js:2
p.extend.each jquery-1.8.2.min.js:2
p.fn.p.each jquery-1.8.2.min.js:2
p.fn.extend.trigger jquery-1.8.2.min.js:2
a.Widget._trigger jquery.mobile-1.3.0.min.js:2
a.widget._createWidget jquery.mobile-1.3.0.min.js:2
(anonymous function) jquery.mobile-1.3.0.min.js:2
a.(anonymous function).(anonymous function) jquery.mobile-1.3.0.min.js:2
(anonymous function) jquery.mobile-1.3.0.min.js:2
p.extend.each jquery-1.8.2.min.js:2
p.fn.p.each jquery-1.8.2.min.js:2
a.fn.(anonymous function) jquery.mobile-1.3.0.min.js:2
C jquery.mobile-1.3.0.min.js:2
a.mobile.loadPage jquery.mobile-1.3.0.min.js:2
a.mobile.changePage jquery.mobile-1.3.0.min.js:2
a.mobile._handleHashChange jquery.mobile-1.3.0.min.js:2
(anonymous function) jquery.mobile-1.3.0.min.js:2
p.event.dispatch jquery-1.8.2.min.js:2
g.handle.h jquery-1.8.2.min.js:2
p.event.trigger jquery-1.8.2.min.js:2
(anonymous function) jquery-1.8.2.min.js:2
p.extend.each jquery-1.8.2.min.js:2
p.fn.p.each jquery-1.8.2.min.js:2
p.fn.extend.trigger jquery-1.8.2.min.js:2
(anonymous function) jquery.mobile-1.3.0.min.js:2