簡體   English   中英

MVC應用程序中的$ .get()和$ .post()在本地Cassini網絡服務器中可以正常工作,但是當發布到IIS 7.5中后,它將不再起作用

[英]$.get() and $.post() in MVC app works fine in local Cassini webserver but when published into IIS 7.5 it doesn't work anymore

我實際上停留在此MVC應用程序上,無法找到原因。

該代碼在本地Vis Studio 2013 Cassini Web服務器中運行良好。

因此,當我在瀏覽器中鍵入URL:“ / Customer / EnterCustomerDetails”時,將顯示一個簡單的表單,其中包含狀態消息“ Loading ...”的DIV標簽,並且在5秒鍾后由於EF返回了一些數據。 $ .get()腳本被執行。

但是,當我將相同的代碼發布到Windows 7 Ultimate中運行的IIS 7.5中時,我看到的只是表單,只有div標簽顯示消息“ Loading ...”,但沒有數據顯示,似乎是$。 get()在完整的IIS中不起作用,$。post()也不起作用。

我要去哪里錯了? 任何幫助,不勝感激。 提前致謝。

“ EnterCustomerDetails.cshtml”

@model P10LearnNewMVCWithEF.ViewModel.CustomerViewModel
@using P10LearnNewMVCWithEF.Models

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>EnterCustomerDetails</title>

    @*BELOW Weve IMPORTED the 3 important JQuery LIBRARIES, since we want to use the $.get() AJAX method*@

    <script src="~/Scripts/jquery-1.8.3.js"></script>
    <script src="~/Scripts/jquery.validate.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.js"></script>

</head>
<body>
    <div>

        <form id="frm1">
            Customer Code:- @Html.TextBoxFor(m => m.Customer.CustomerCode)

            @Html.ValidationMessageFor(x => x.Customer.CustomerCode) <br />

            Customer Name:- @Html.TextBoxFor(m => m.Customer.CustomerName)
            @Html.ValidationMessageFor(x => x.Customer.CustomerName) <br />
            <input type="button" value="Submit via true AJAX" id="btn1" onclick="SendData()" />
        </form>

        <div id="status"></div>
        <table id="tbl">
            <tr><th>Customer Code</th><th>Customer Name</th></tr>
        </table>

        <script type="text/javascript">

            $("#status").text("Loading..."); //Add STATUS MESSAGE "Loading..." to DIV. Must use ".text" and NOT ".val"

            //Making a CALL to the "GetCustomers" ACTION within the "Customer" CONTROLLER and Results returned will be in "BindData"
            $.get("GetCustomers", null, BindData);

            // The "GetCustomers" ACTION will return the JSON data into this JavaScript function
            function BindData(customers) {

                var tbl = $("#tbl");

                for (var i = 0; i < customers.length; i++) {
                    var newRow = "<tr>" +
                        "<td>" + customers[i].CustomerCode + "</td>" +
                        "<td>" + customers[i].CustomerName + "</td>" +
                        "</tr>";

                    tbl.append(newRow);
                }

                $("#status").text(""); //REMOVING STATUS MESSAGE to EMPTY
            }

            function SendData() {
                $("#status").text("Adding data via ajax..."); //Add STATUS MESSAGE "Loading..." to DIV. Must use ".text" and NOT ".val"

                var frm = $("#frm1").serialize();

                $.post("Submit", frm, BindData);

                $("#Customer_CustomerCode").val("");
                $("#Customer_CustomerName").val("");
            }
        </script>

    </div>
</body>
</html>

“ CustomerController.cs”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using P10LearnNewMVCWithEF.DAL; //"CustomerDal" in here which derives from "DbContext"
using P10LearnNewMVCWithEF.Models; //"Customer" class in here
using P10LearnNewMVCWithEF.ViewModel;

namespace P10LearnNewMVCWithEF.Controllers
{
    public class CustomerController : Controller
    {
        //Below is our NEW Simplified ACTION that will simply display our Enter Customer Details UI
        public ActionResult EnterCustomerDetails()
        {
            CustomerViewModel objCustomerViewModel = new CustomerViewModel();
            objCustomerViewModel.Customer = new Customer(); //This code by SHIV is pretty useless

            //NOTE - WE'RE NOT RETRIEVING ANY DATA FROM DATABASE ANYMORE! as we'll do that with "GetCustomers()" ACTION
            return View("EnterCustomerDetails", objCustomerViewModel);
        }

        public ActionResult Submit()
        {
            Customer obj = new Customer();

            obj.CustomerCode = Request.Form["Customer.CustomerCode"]; //"name" is used on Server-side. "id" is used in Client-side.
            obj.CustomerName = Request.Form["Customer.CustomerName"];

            CustomerDal dal = new CustomerDal();

           if (ModelState.IsValid)
            {
                //Let's INSERT the new Customer into DB via EF
                dal.Customers.Add(obj);

                dal.SaveChanges();
            }
            List<Customer> customersColl = dal.Customers.ToList<Customer>(); //Return all data from tCustomer via EF

            return Json(customersColl, JsonRequestBehavior.AllowGet); //make sure you say customersColl AND NOT “customersColl”
        }

        //Below: "GetCustomers()" ACTION will be called by $.get() and simply returns a Collection of JSON data
        public ActionResult GetCustomers()
        {
            CustomerDal dal = new CustomerDal();
            List<Customer> customerscoll = dal.Customers.ToList<Customer>();
            Thread.Sleep(5000);

            return Json(customerscoll, JsonRequestBehavior.AllowGet);
        }
    }
}

將其發布為答案,以便您可以將其標記為已回答。

這聽起來像您的IIS應用程序池中的身份未獲得數據庫訪問權限。 分配具有權限的新身份,或授予對您正在使用的身份的訪問權限。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM