简体   繁体   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

I'm actually stuck on this MVC application and can't find out the cause. 我实际上停留在此MVC应用程序上,无法找到原因。

The code works fine in local Vis Studio 2013 Cassini webserver. 该代码在本地Vis Studio 2013 Cassini Web服务器中运行良好。

Hence when I type the URL in the browser: "/Customer/EnterCustomerDetails" then I'm displayed a simple form, a DIV tag containing status message "Loading..." and after 5 seconds some data via EF is returned due to the $.get() script that gets executed. 因此,当我在浏览器中键入URL:“ / Customer / EnterCustomerDetails”时,将显示一个简单的表单,其中包含状态消息“ Loading ...”的DIV标签,并且在5秒钟后由于EF返回了一些数据。 $ .get()脚本被执行。

However, when I publish the same code into IIS 7.5 running inside Windows 7 Ultimate, then all I see is the form and only the div tag that displays the message "Loading..." but no data is displayed and it seems the $.get() is not working in the full blown IIS and neither is $.post(). 但是,当我将相同的代码发布到Windows 7 Ultimate中运行的IIS 7.5中时,我看到的只是表单,只有div标签显示消息“ Loading ...”,但没有数据显示,似乎是$。 get()在完整的IIS中不起作用,$。post()也不起作用。

Where am I going wrong? 我要去哪里错了? Any help much appreciated. 任何帮助,不胜感激。 Thanks in advance. 提前致谢。

"EnterCustomerDetails.cshtml" “ 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" “ 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);
        }
    }
}

Posting this as an answer so that you can mark it as answered. 将其发布为答案,以便您可以将其标记为已回答。

That sounds like the identity in your IIS app pool is not authorized for database access. 这听起来像您的IIS应用程序池中的身份未获得数据库访问权限。 Either assign a new identity that has permission, or grant access to the identity you are using. 分配具有权限的新身份,或授予对您正在使用的身份的访问权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MVC App可在IIS 7中运行,而不能在Cassini中运行(IIS 7也可) - MVC App works in IIS 7, does not work in Cassini (IIS 7, too) MVC 4-上载到IIS 7.5后在IE中不起作用 - MVC 4 - Upload Doesn't Work in IE When deployed to IIS 7.5 WebApplication 在 IIS Express 上运行良好,但在本地 IIS 上不起作用 - WebApplication works fine on IIS Express but it doesn't on Local IIS 将网站发布到本地IIS 7.5 - Published website to local IIS 7.5 使用POST方法的MVC 4发布不起作用(使用GET方法-有效) - MVC 4 posting with POST method doesn't work (with GET method - works) 当 ASP 应用程序托管在 IIS Exress 中时,可以连接到 SQL 数据库,但在本地 IIS 中托管时却无法连接 - Connecting to SQL database works when ASP application is hosted in IIS Exress but doesn't work when hosted in Local IIS 在IIS 7.5上托管MVC应用 - Hosting MVC App on IIS 7.5 在将新视图页面MVC 5发布到IIS 8.5时接收错误页面,可以在本地调试良好 - Receiving error page when publishing new view pages MVC 5 to IIS 8.5 , works fine debugging on local ASP.NET MVC模型绑定不适用于AJAX GET,但适用于Post - ASP.NET MVC Model binding doesn't work with AJAX GET but works with Post 部署在IIS 7.5上的MVC应用程序不写入日志文件 - MVC app not writing log files when deployed on IIS 7.5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM