我有一個簡單的ajax請求,我試圖調用我創建的Web服務。 Web服務代碼永遠不會被執行。我從網上得到這個代碼示例,我想也許我打電話錯誤的jQuery我不知道?調用Web服務jquery
這裏是jQuery的
<script type="text/javascript">
$(document).ready(function() {
alert("Hi");
$("#btnTitleQuery").bind("click", function() {
$("#query_results").empty();
$("#query_results").append('<table id="ResultsTable" class="BooksTable"><tr><th>BookNum</th><th>Title</th><th>Author</th></tr>');
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "BookServices.asmx/GetBooksByTitle",
data: '{ strTitle: "' + $("#txtTitle").val() + '" }',
dataType: "json",
success: function(msg) {
var c = eval(msg.d);
for (var i in c) {
$("#ResultsTable tr:last").after("<tr><td>" + c[i][0] + "</td><td>" + c[i][1] + "</td><td>" + c[i][2] + "</td></tr>");
}
}
});
})
$("#btnAuthorQuery").bind("click", function() {
$("#query_results").empty();
$("#query_results").append('<table id="ResultsTable" class="BooksTable"><tr><th>BookNum</th><th>Title</th><th>Author</th></tr>');
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "BookServices.asmx/GetBooksByAuthor",
data: '{ strAuthor: "' + $("#txtAuthor").val() + '" }',
dataType: "json",
success: function(msg) {
var c = eval(msg.d);
for (var i in c) {
$("#ResultsTable tr:last").after("<tr><td>" + c[i][0] + "</td><td>" + c[i][1] + "</td><td>" + c[i][2] + "</td></tr>");
}
}
});
})
});
</script>
該網站的服務是我的根目錄。
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.Odbc;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
namespace TryWillJSON
{
[WebService(Description = "Web services to query the book database.", Namespace = "http://www.williamsportwebdeveloper.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class BookServices : System.Web.Services.WebService {
public BookServices() {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod(Description = "Gets the books matching part of a title.")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetBooksByTitle(string strTitle) {
OdbcConnection objConnection = new OdbcConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Books"].ConnectionString);
OdbcCommand objCommand = new OdbcCommand("SELECT * FROM reading WHERE Title LIKE '%" + strTitle + "%' ORDER BY BookNum;", objConnection);
DataSet objDataSet = new DataSet();
OdbcDataAdapter objDataAdapter = new OdbcDataAdapter(objCommand);
objDataAdapter.Fill(objDataSet, "reading");
objConnection.Close();
// Create a multidimensional jagged array
string[][] JaggedArray = new string[objDataSet.Tables[0].Rows.Count][];
int i = 0;
foreach (DataRow rs in objDataSet.Tables[0].Rows)
{
JaggedArray[i] = new string[] { rs["BookNum"].ToString(), rs["Title"].ToString(), rs["Author"].ToString() };
i = i + 1;
}
// Return JSON data
JavaScriptSerializer js = new JavaScriptSerializer();
string strJSON = js.Serialize(JaggedArray);
return strJSON;
}
[WebMethod(Description = "Gets the books matching part of an author's name.")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetBooksByAuthor(string strAuthor)
{
OdbcConnection objConnection = new OdbcConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Books"].ConnectionString);
OdbcCommand objCommand = new OdbcCommand("SELECT * FROM reading WHERE Author LIKE '%" + strAuthor + "%' ORDER BY BookNum;", objConnection);
DataSet objDataSet = new DataSet();
OdbcDataAdapter objDataAdapter = new OdbcDataAdapter(objCommand);
objDataAdapter.Fill(objDataSet, "reading");
objConnection.Close();
// Create a multidimensional jagged array
string[][] JaggedArray = new string[objDataSet.Tables[0].Rows.Count][];
int i = 0;
foreach (DataRow rs in objDataSet.Tables[0].Rows)
{
JaggedArray[i] = new string[] { rs["BookNum"].ToString(), rs["Title"].ToString(), rs["Author"].ToString() };
i = i + 1;
}
// Return JSON data
JavaScriptSerializer js = new JavaScriptSerializer();
string strJSON = js.Serialize(JaggedArray);
return strJSON;
}
}
}
我可以看到警報命中,但在螢火當我檢查什麼在jQuery函數螢火蟲的「味精」參數告訴我這「的ReferenceError:味精是沒有定義」
這裏是哪裏我得到的代碼: williamsportwebdeveloper.com/cgi/wp/?p=494
如果有人有時間,你可以看到,如果你可以在Visual Studio
這裏正確設置它是一個小提琴我的設置以及它的價值 http://jsfiddle.net/npl77/U33XS/
是在ASP.NET一些可怕的空白。請告訴我,複製和粘貼代碼會破壞你的空白。 – Raynos 2011-03-15 18:55:40