我正在尝试使用通过ajax GET返回到mySQL数据库表的数据填充下拉列表。
如果我尝试在浏览器中导航到/CustomerService.svc,则会显示该页面,但是,如果我尝试转到/CustomerService.svc/GetCustomers,则会收到错误状态代码404:找不到URL /资源加载失败。
从Chrome开发者工具控制台复制的我的错误:
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
[这是Chrome中的DevTools控制台。] [1]
[这是我通过ajax请求URL时显示的错误页面。] [2]
码:
//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;
}
}
我也尝试将网址作为'h.t.tp://localhost: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>
最佳答案
因此,我最终创建了一个空项目并将代码复制到该项目,然后我的Jquery代码开始工作。 Visual Studios一定有问题。
关于c# - 尝试调用Web服务中的函数时,Ajax GET请求返回404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43645589/