1

jQueryからアクションを呼び出そうとしています。アクションはtrueまたはfalseを返す必要があり、戻り値に基づいて何かを実行します。

私は次のコードを持っています。

 $("#payment").click(function (e) {
       if($("#deliverytime").attr("disabled")){
            //something here...   
        }
        else
        {
            var postcode=$('#Postcode').val();
            var restId='<%:ViewBag.RestaurantId %>';
            var URL = "/Restaurant/DoesRestaurantDeliver?restid="+restId+"&&postcode="+postcode;

            $.ajax({
                 type: "GET",
                 url: URL
             }).done(function(msg) {
                        if(msg==true){
                             $('#commonMessage').html(msg);
                             $('#commonMessage').delay(400).slideDown(400).delay(4000).slideUp(400);
                             return false;
                        }
                        else{
                            $('#commonMessage').html(msg);
                             $('#commonMessage').delay(400).slideDown(400).delay(4000).slideUp(400);
                             return false;
                        }
            }); 



        }
    });

コードが機能していません。それは言う'msg' is not defined。これは、jQuery-ajaxを使用してこれを行う方法ではありませんか?私は何が間違っているのですか?

編集:

コントローラのアクション

public JsonResult DoesRestaurantDeliver(Int32 restid, string postcode)
    {

        if (rest.getDeliveryPriceForPostcode(restid, postcode) == null)
        {
            return Json(Boolean.FalseString, JsonRequestBehavior.AllowGet);
        }
        else
            return Json(Boolean.TrueString, JsonRequestBehavior.AllowGet);
    }

変更された機能

var postcode = $('#DeliveryInfo_Postcode').val();
            var restId = '<%:ViewBag.RestaurantId %>';
            var URL = "/Restaurant/DoesRestaurantDeliver?restid=" + restId + "&&postcode=" + postcode;
            $.ajax({
                url: URL,
                dataType: "json",
                type: "GET",
                data: "{}",
                success: function (data) {
                    if (!data.hasError) {
                        $('#commonMessage').html(data);

                        return false;
                    }
                }
            });

編集-2

<script>
$(document).ready(function () {
    $("#checkout").click(function (e) {
        getDeliveryInfo();
    });
});
function getDeliveryInfo() {
    var URL ="/Restaurant/DoesRestaurantDeliver/6/2259"
    $.get(URL, function (data) {
        alert(data.isValid);
    });
}
</script>
<%using (Html.BeginForm())
 { %>
<input type="submit" name="submit" id="checkout"/>
<%} %>

上記のコードは機能しません。しかし、以下のようにフォームの外側に「チェックアウト」ボタンを配置すると、機能します。

<%using (Html.BeginForm())
 { %>

<%} %>
<input type="submit" name="submit" id="checkout"/>
4

1 に答える 1

1

以下のようにajax呼び出しを構成できます。

あなたのajaxコールが.done(function(msg) {}エリアで問題を抱えているように感じます。

以下のように設定してみてください success: function (data) {}

<script type="text/javascript">

    $(function () {

        var companyName = $("#companyname");

        $.ajax({
            url: "/Company/GetName",
            dataType: "json",
            type: "GET",
            data: "{}",
            success: function (data) {
                if (!data.hasError) {
                    companyName.html(data.companyName);
                }
            }
        });
    });

</script>

編集

アクション方法は以下のようになります(これはサンプルです。状況に応じて調整してください)

 [HttpGet]
 public JsonResult GetName(string term)
        {
            var result = Repository.GetName(term);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

編集2

匿名オブジェクト(isValid)を使用します( JSONはキーと値のペアであることを忘れないでください):

アクション方法

   [HttpGet]
   public JsonResult DoesRestaurantDeliver(Int32 restid, string postcode)
    {
       if (rest.getDeliveryPriceForPostcode(restid, postcode) == null)
        {
            return Json(new { isValid = false},JsonRequestBehavior.AllowGet);
        }
        else
            return Json(new { isValid = true },JsonRequestBehavior.AllowGet);
    }

そして、ajaxメソッドの内部:

success: function(data) {
    if (data.isValid) {
        ...
    }
}

編集3

ボタンの種類を次のように変更するだけです(フォームから値を送信しないため)

<input type="button" name="submit" id="checkout"/>
于 2012-12-14T14:38:30.243 に答える