首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当尝试调用位于web服务中的函数时,ajax GET请求返回404

当尝试调用位于web服务中的函数时,ajax GET请求返回404
EN

Stack Overflow用户
提问于 2017-04-27 06:32:02
回答 1查看 115关注 0票数 1

我正在尝试用通过ajax GET to mySQL数据库表返回的数据填充一个下拉列表。如果我尝试在浏览器中导航到/CustomerService.svc,页面就会出现,但是如果我尝试转到/CustomerService.svc/GetCustomers,我就会得到错误状态代码404: url not found/resource to load。我从Chrome Dev Tools控制台复制的错误:

代码语言:javascript
复制
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控制台。这是我通过ajax请求URL时显示的错误页面。

代码:

代码语言:javascript
复制
//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;
                }

            }

我也尝试过将error.This作为'h.t.tp://localhost:54522/CustomerService.svc/GetCustomers‘,并在项目解决方案资源管理器中获得相同的url。

在Web.config中:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2017-04-28 09:08:27

因此,我最终创建了一个空项目,并将代码复制到其中,然后我的Jquery代码开始工作。一定是Visual Studios出了什么问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43645589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档