JSON调用后误差函数

JSON调用后误差函数

本文介绍了JSON调用后误差函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的jQuery的电话后这在某种程度上被调用的误差函数,即使它把数据输入数据库完美的罚款。

 <脚本类型=文/ JavaScript的>
    $(文件)。就绪(函数(){
        $(#ExtMailCreate)。点击(函数(){
            VAR URL ='@ Url.Action(创建);
            VAR数据= JSON.stringify(SaveExternalMail());
            $阿贾克斯({
                键入:POST,
                网址:网址,
                数据:数据,
                数据类型:JSON
                的contentType:应用/ JSON的;字符集= UTF-8,
                成功:功能(数据){
                    $(#结果)追加(通行证);
                },
                错误:功能(数据){
                    $(#结果)追加(错误)。
                }
            });
        });
    });    功能SaveExternalMail(){
        。VAR costcentre code = $(#CostCentre code)VAL();
        。VAR天= $(#ExtMailDatePicker)VAL();
        变种externalMailItemID = $(#ExternalMailItemID)VAL();
        变种externalMailLocationID = $(#ExternalMailLocationID)VAL();
        变种数量= $(#数量)VAL()。
        返回{
            CostCentre code:costcentre code,
            日:当天,
            ExternalMailItemID:externalMailItemID,
            ExternalMailLocationID:externalMailLocationID,
            数量:数量
        };
    }
< / SCRIPT>

- 控制器

 < HttpPost()> _
    功能创建(BYVAL收集作为ExternalMail)由于JsonResult
        尝试
            如果ModelState.IsValid然后
                _repositoryForExternalMail.Save(收集,)
            万一            返回JSON(新ExternalMail随着{.CostCentre code = collection.CostCentre code,.DAY = collection.Day,.Quantity = collection.Quantity},JsonRequestBehavior.AllowGet)        抓住EX为例外
            返回JSON(一个错误发生)
        结束Try
    结束功能

----更新---

谷歌浏览器报告以下错误:

-- Page Source

 <!DOCTYPE html>
 <html>
 <head>
<meta charset="utf-8" />
<title>Create</title>
<link href="/Mail/Content/Site.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="/Mail/Scripts/jqModal.css" />
<link href="http://cdn.kendostatic.com/2012.2.913/styles/kendo.common.min.css" rel="stylesheet" type="text/css" />
<link href="http://cdn.kendostatic.com/2012.2.913/styles/kendo.dataviz.min.css" rel="stylesheet" type="text/css" />
<link href="http://cdn.kendostatic.com/2012.2.913/styles/kendo.blueopal.min.css" rel="stylesheet" type="text/css" />

     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js">  </script>
 <script src="/Mail/Scripts/json2min.js" type="text/javascript"></script>
 <script src="/Mail/Scripts/jquery.JSONP.min.js" type="text/javascript"></script>
 <script src="/Mail/Scripts/jquery.validate.min.js" type="text/javascript"></script>
 <script src="/Mail/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript">         </script>

<script src="http://cdn.kendostatic.com/2012.2.913/js/kendo.all.min.js" type="text/javascript"></script>
<script src="http://cdn.kendostatic.com/2012.2.913/js/kendo.aspnetmvc.min.js" type="text/javascript"></script>
<script src="/Mail/Scripts/modernizr-1.7.min.js" type="text/javascript"></script>
<script type="text/javascript" src="/Mail/Scripts/jqModal.js"></script>

 </head>
 <body>
<div class="page">
    <header>
  <div id="pageheader">
        <div id="pageheader_left">
            <img id="TitleBanner" src=/Mail/Content/Images/titlebanner.jpg alt="TitleBanner"/>
        </div>
        <div id="pageheader_center">

        </div>
        <div id="pageheader_right">
         <a href="/Mail/" title="Click to go to Home Page"><img id="HomeImage" src=/Mail/Content/Images/home.gif alt="Home"/></a>
         <a href="/Mail/Account/About"><img id="AboutImage" src=/Mail/Content/Images/about.gif alt="About"/></a>
          <a href="/Mail/Account/Logoff"><img id="ExitImage" src=/Mail/Content/Images/logoff.gif alt="Exit"/></a>
         </div>
        <div id="pageheader_lower">
        </div>
    </div>


     <ul class="k-widget k-reset k-header k-menu" id="Menu"><li class="k-item k-state-default">
 <a class="k-link" href="/Mail/">Home</a></li><li class="k-item k-state-default"><span class="k-link">Manage<span class="k-icon k-i-arrow-s"></span></span><ul class="k-group"><li class="k-item k-state-default"><span class="k-link">External Mail<span class="k-icon k-i-arrow-e"></span></span><ul class="k-group"><li class="k-item k-state-default"><a class="k-link" href="/Mail/ExternalMail">External Mail</a></li><li class="k-item k-state-default"><a class="k-link" href="/Mail/ExternalMailCategory">External Mail Category</a></li><li class="k-item k-state-default"><a class="k-link" href="/Mail/ExternalMailItem">External Mail Item</a></li><li class="k-item k-state-default"><a class="k-link" href="/Mail/ExternalMailCost">External Mail Cost</a></li><li class="k-item k-state-default"><a class="k-link" href="/Mail/ExternalMailLocation">External Mail Location</a></li><li class="k-item k-state-default"><a class="k-link" href="/Mail/ExternalMailInvoice">External Mail Invoice</a></li></ul></li><li class="k-item k-state-default"><span class="k-link">Internal Mail<span class="k-icon k-i-arrow-e"></span></span><ul class="k-group"><li class="k-item k-state-default"><a class="k-link" href="/Mail/InternalMail">Internal Mail</a></li><li class="k-item k-state-default"><a class="k-link" href="/Mail/InternalMailCategory">Internal Mail Category</a></li><li class="k-item k-state-default"><a class="k-link" href="/Mail/InternalMailItem">Internal Mail Item</a></li><li class="k-item k-state-default"><a class="k-link" href="/Mail/InternalMailCost">Internal Mail Cost</a></li><li class="k-item k-state-default"><a class="k-link" href="/Mail/InternalMailLocation">Internal Mail Location</a></li></ul></li><li class="k-item k-state-default"><a class="k-link" href="/Mail/CostCentre">Cost Centre</a></li></ul></li><li class="k-item k-state-default"><span class="k-link">Reports</span></li></ul><script>
jQuery(function(){jQuery("#Menu").kendoMenu({});});
 </script>
    </header>
    <section id="main">

 <div class="jqmWindow" id="dialog">
     <a href="#" class="jqmClose">Close</a>
 </div>


 <script type="text/javascript">

$(document).ready(function () {

    var exturl = '/Mail/ExternalMail/Create';
    $("#ExtMailCreate").click(function () {


        var dataJSON = JSON.stringify(SaveExternalMail());

        $.ajax({
                    cache: false,
                        type: "POST",
                        url: exturl,
                        data: dataJSON,
                        contentType: 'application/json; charset=utf-8',
                        success: function (data) {
                            showMessage("test", "information");
                        },
                        error: function (xhr, textStatus, errorThrown) {
                            showMessage("An error occurred while adding this record.           Please try again later.", "error");
                        }


                    });

    });

});

function showMessage(message, messageType) {
    $().ready(function () {
        $('#dialog').jqm();

        switch (messageType) {
            case 'information':
                $('#dialog').removeClass("errorMessage");
                $('#dialog').addClass("informationMessage");
                break;
            case 'error':
                $('#dialog').removeClass("informationMessage");
                $('#dialog').addClass("errorMessage");
                break;
            default:
                break;
        }

        $('#dialog').html("<a href=\"#\" class=\"jqmClose\">Close</a><br />" + message);
        $('#dialog').jqmShow();
    });
}


    function SaveExternalMail() {
        var costcentrecode = $("#CostCentreCode").val();
        var day = $("#ExtMailDatePicker").val();
        var externalMailItemID = $("#ExternalMailItemID").val();
        var externalMailLocationID = $("#ExternalMailLocationID").val();
        var quantity = $("#Quantity").val();

        return { CostCentreCode: costcentrecode, Day: day, ExternalMailItemID:      externalMailItemID, ExternalMailLocationID: externalMailLocationID, Quantity: quantity };
    }

 </script>
 <form action="/Mail/ExternalMail/Create" method="post">    <fieldset>
    <legend>ExternalMail</legend>
    <div class="editor-label">
        <label for="ExternalMailLocationID">Please select a Location</label>
    </div>
    <div class="editor-field">
        <select data-val="true" data-val-number="The field ExternalMailLocationID must be a number." data-val-required="The ExternalMailLocationID field is required." id="ExternalMailLocationID" name="ExternalMailLocationID">
 <option value="2">Sydney</option>
 </select>
        <span class="field-validation-valid" data-valmsg-for="ExternalMailLocationID" data-valmsg-replace="true"></span>
    </div>
    <div class="editor-label">
        <label for="CostCentreCode">Please select a CostCentre</label>
    </div>
    <div class="editor-field">
        <select id="CostCentreCode" name="CostCentreCode" size="15"><option value="Test000">Test</option>
      </select>
        <span class="field-validation-valid" data-valmsg-for="CostCentreCode" data-valmsg-replace="true"></span>
    </div>
         <div class="editor-label">
             <label for="ExternalMailItemID">Please select a Mail Item Type</label>
         </div>
         <div class="editor-field">
             <select data-val="true" data-val-number="The field ExternalMailItemID must be a number." data-val-required="The ExternalMailItemID field is required." id="ExternalMailItemID" name="ExternalMailItemID"><option value="4">Test10</option>
      </select>
        <span class="field-validation-valid" data-valmsg-for="ExternalMailItemID" data-valmsg-replace="true"></span>
    </div>
    <div class="editor-label">
        <label for="Day">Please select a Date</label>
    </div>
    <div class="editor-field">
        <input class="k-input" id="ExtMailDatePicker" name="ExtMailDatePicker" type="date" value="19/03/2013" /><script>
     jQuery(function(){jQuery("#ExtMailDatePicker").kendoDatePicker({"format":"d/MM/yyyy","min":new Date(1900,0,1,0,0,0,0),"max":new Date(2099,11,31,0,0,0,0)});});
      </script>
        <span class="field-validation-valid" data-valmsg-for="Day" data-valmsg-replace="true"></span>
    </div>
    <div class="editor-label">
        <label for="Quantity">Please enter the no of Mails</label>
    </div>
    <div class="editor-field">
        <input class="text-box single-line" data-val="true" data-val-number="The field Quantity must be a number." data-val-required="The Quantity field is required." id="Quantity" name="Quantity" type="text" value="" />
        <span class="field-validation-valid" data-valmsg-for="Quantity" data-valmsg-replace="true"></span>
    </div>
    <p>
        <input type="button" value="Create" id="ExtMailCreate" />
    </p>
</fieldset>
 </form><div>
<a href="/Mail/ExternalMail">Back to List</a>
 </div>
      <div id="result">
 </div>

    </section>
    <footer>

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

-- Ajax Request Respone

Request URL:http://localhost/Mail/ExternalMail/Create
Request Method:POST
Status Code:403 Forbidden
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Authorization:Negotiate    TlRMTVNTUAADAAAAAAAAAEgAAAAAAAAASAAAAAAAAABIAAAAAAAAAEgAAAAAAAAASAAAAAAAAABIAAAABcKIogUBKAoAAAAP
Connection:keep-alive
Content-Length:113
Content-Type:application/json; charset=UTF-8
Cookie:ASP.NET_SessionId=tg1xrabaigumj3fxooa1satc
DNT:1
Host:localhost
Origin:http://localhost
Referer:http://localhost/Mail/externalmail/Create
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko)     Chrome/25.0.1364.172 Safari/537.22
X-Requested-With:XMLHttpRequest
Request Payload
{"CostCentreCode":"B125","Day":"20/03/2013","ExternalMailItemID":"4","ExternalMailLocationID":"1","Quantity":"2"}
Response Headersview source
Cache-Control:private, s-maxage=0
Content-Length:127
Content-Type:application/json; charset=utf-8
Date:Wed, 20 Mar 2013 01:31:21 GMT
Server:Microsoft-IIS/5.1
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:3.0
X-Powered-By:ASP.NET
解决方案

I think the problem is specifying JsonRequestBehavior.AllowGet

and then posting to the method:

$.ajax({
  type: "POST",
  url: url,

If the database is getting populated, we know the repository code is firing. The browser is reporting an HTTP403 which smells like the framework is throwing a security exception, which it may do if it's expecting to respond to GET and receives a POST.

这篇关于JSON调用后误差函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 23:10