簡體   English   中英

在ajax生成的表單中調用jquery.ajax

[英]Call jquery.ajax within ajax generated form

我這里有點問題。 我想要實現的用jquery / ajax生成模態/表單的方法,並且在該模態內應該是另一個ajax調用(由2個特定的id生成)。

希望這可以使您更加清楚:

我得到了我的頁面,然后按“生成表單”。 然后,jQuery通過ajax加載模態並獲取url:“ / modal / id1 / id2”(整個表單已加載)。 這仍然有效。 現在,我希望在此表單中使用一個按鈕“ post form”,將該按鈕發布到相同的URL“ / modal / id1 / id2”,並使用“ inner” ajax調用的結果覆蓋當前模態。

我收到此錯誤:

POST http://localhost/modal/id1/id2 500 (Internal Server Error) jquery.min.js:6
send jquery.min.js:6
x.extend.ajax jquery.min.js:6
(anonymous function) VM7593:4
x.event.dispatch jquery.min.js:5

看來ajax無法正常工作。 這是我使用的一些代碼:此代碼生成第一個模態。 仍然可以正常工作:

    <script>
      $('[data-toggle="view"]').on('click',function(e) {
        e.preventDefault();
        var id1 = $(this).data('id1');
        var id2 = "<?php echo $id2; ?>"
        $.ajax({
           type: "GET",
           url: "offers/"+id1+"/create/"+id2,
           dataType: "html",
           success: function(response){
             $('#offerModal').html(response);
             $('#offerModal').modal('toggle');
           }
        });
      });
    </script>

這是第一個ajax請求返回的代碼,並顯示在模式中:

 <script>
   $('#createOffer').on('click',function(){
     $.ajax({
             type: "post",
             url: "offers/1/create/2", //url exists and is working
             data: { test: "test" },
             success: function(response){
               alert(response);
             }
          });
      });
   </script>

希望您能(理解我的問題並)幫助我。

謝謝!

也許未設置$('#createOffer')點擊事件? 也許將其包裝在$(document)ready函數中會有所幫助:

    $(document).ready(function() {
        $('#createOffer').on('click',function(){
            $.ajax({
                type: "post",
                url: "offers/1/create/2", //url exists and is working
                data: { test: "test" },
                success: function(response){
                   alert(response);
                }
            });
        });
    });

感謝您的幫助。 我自己找到了答案(感謝ADASein的一點提示),但我犯了一個大錯誤。 :/我發布的代碼可以正常工作,但是發布網址的視圖在該特定星座中返回了錯誤。

暫無
暫無

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

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