1
我想填充一個下拉列表,其中包含通過ajax GET返回到mySQL數據庫表的數據。 如果我嘗試在瀏覽器中導航到/CustomerService.svc,那麼該頁面顯示,但如果我嘗試去/CustomerService.svc/GetCustomers,那麼我得到錯誤狀態代碼404:未找到URL /資源加載失敗。 從Chrome開發工具控制檯複製我的錯誤:ajax當嘗試調用位於Web服務中的函數時返回404請求返回404
GET http://localhost:54522/CustomerService.svc/GetCustomer 404 (Not Found)
send @ jquery.min.js:2
ajax @ jquery.min.js:2
CustomerDropDown @ Customer.js:142
(anonymous) @ Customer.js:13
dispatch @ jquery.min.js:2
h @ jquery.min.js:2
[這是在鍍鉻的DevTools控制檯。] [1] [[這是當我請求通過Ajax的URL示出的錯誤頁面。] 2]
CODE:
//In Customer.js:
$("#edit-tab").click(function() {
CustomerDropDown('inputCustomerSelect');
});
function CustomerDropDown(elementId) {
var element = "#" + elementId;
var removeOptions = element + " option";
$(removeOptions).remove();
$.ajax({
url: '/CustomerService.svc/GetCustomers',
method: 'GET',
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function (data) {
var customers = data;
var selectHtml = "";
selectHtml += "<option value='0'> Select Customer </option>";
customers.forEach(function (customer) {
selectHtml += "<option value='" + customer.id + "'>" + customer.companyName + "</option>";
});
$(element).append(selectHtml);
},
fail: function (data) {
alert("Failed to change customer due to:" + data.d);
},
error: function (jqXHR, status, error) {
alert(jqXHR.statusText);
//alert('Status Code=' + jqXHR.status + ' Status=' + status + ' Error=' + error);
}
});
}
//In CustomerService.svc:
namespace DPQ_AdminDatabaseEditor_WebApp
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class CustomerService
{
// To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
// To create an operation that returns XML,
// add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
// and include the following line in the operation body:
// WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
// Add more operations here and mark them with [OperationContract]
[OperationContract]
[WebInvoke(Method = "GET",
BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Json
)]
public List<Customer> GetCustomers()
{
CustomerDB customerDb = new CustomerDB();
List<Customer> costumers = customerDb.getCustomerList;
return costumers;
}
}
}
//In CustomerDB.cs:
public List<Customer> getCustomerList
{
get
{
MySqlConnection connection = new MySqlConnection(mySQLConnStr);
string sel = "SELECT * " +
"FROM customer " +
"ORDER BY name ASC";
MySqlCommand command = new MySqlCommand(sel, connection);
try
{
connection.Open();
customerList = new List<Customer>();
MySqlDataReader mSqlReader = command.ExecuteReader();
DataTable dtList = new DataTable();
dtList.Load(mSqlReader);
foreach (DataRow row in dtList.Rows)
{
int id = Convert.ToInt32(mSqlReader["id"]);
string compName = Convert.ToString(row["name"]);
decimal markup = Convert.ToInt32(row["markup"]);
decimal adder = Convert.ToInt32(row["adder"]);
bool active = Convert.ToBoolean(row["active"]);
Customer record = new Customer
{
id = id,
companyName = compName,
markup = markup,
adder = adder,
active = active
};
customerList.Add(record);
}
connection.Close();
}
catch (MySqlException ee)
{
connection.Close();
Console.Write(ee.Message.ToString());
return null;
}
return customerList;
}
}
我也試過網址的 'http://本地主機:54522/CustomerService.svc/GetCustomers的',並得到了同樣的錯誤[這是該項目。解決方案資源管理器]。[3]
在web.config中:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="DPQ_AdminDatabaseEditor_WebApp.CustomerServiceAspNetAjaxBehavior">
<webHttp />
</behavior>
<behavior name="DPQ_AdminDatabaseEditor_WebApp.VendorServiceAspNetAjaxBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<services>
<service name="DPQ_AdminDatabaseEditor_WebApp.CustomerService">
<endpoint address="" behaviorConfiguration="DPQ_AdminDatabaseEditor_WebApp.CustomerServiceAspNetAjaxBehavior"
binding="webHttpBinding" contract="DPQ_AdminDatabaseEditor_WebApp.CustomerService" />
</service>
<service name="DPQ_AdminDatabaseEditor_WebApp.VendorService">
<endpoint address="" behaviorConfiguration="DPQ_AdminDatabaseEditor_WebApp.VendorServiceAspNetAjaxBehavior"
binding="webHttpBinding" contract="DPQ_AdminDatabaseEditor_WebApp.VendorService" />
</service>
</services>
</system.serviceModel>