2017-06-02 121 views
1

我想,當我點擊一個jQuery UI的對話框Save按鈕發送一個AJAX請求,這是我正在做它:

$(function() { 
    var comment_dlg = $('#add_comment_dialog'); 
    var quote_id = $('#comment_quote_id'); 

    $('#order_push').click(function() { 
     quote_id.val($(this).data('id')); 
     comment_dlg.dialog('open'); 
    }); 

    comment_dlg.dialog({ 
     title: "Write a comment", 
     autoOpen: false, 
     modal: true, 
     width: 600, 
     height: 300, 
     buttons: { 
      Cancel: function() { 
       $(this).dialog('close'); 
      }, 
      'Save': function() { 
       $.ajax({ 
        url: Routing.generate('push_order_xml'), 
        method: 'GET', 
        data: { quote_id: quote_id }, 
        cache: false 
       }).done(function (data, textStatus, jqXHR) { 
        if (data.success.length) { 
         alert(data.success); 
        } else { 
         alert('Something went wrong!'); 
        } 
       }); 
      } 
     } 
    }); 
}); 

但我收到此錯誤:

Uncaught TypeError: Illegal invocation

我我不確定問題出在哪裏。我查了幾次jQuery UI對話框和jQuery $ .ajax文檔,我的代碼似乎是正確的。

任何想法?

回答

0

好的,最後,並感謝this answer我想出問題來自哪裏。

第一件事,正因爲如此,:

var quote_id = $('#comment_quote_id') 

quote_id值是整個HTML上沒有價值我預計。

其次,我是一個值分配給$('#comment_quote_id')就在這裏:

quote_id.val($(this).data('id')); 

這是正確的。

第三,又是我的錯誤是在這裏使用quote_id

data: { quote_id: quote_id } 

這是 - 再 - 錯誤的,因爲是整個HTML而不是價值本身。

的解決方案,使用quote_id.val()例如:

data: { quote_id: quote_id.val() } 

我不能使用processData: false,因爲我不想通過HTML而是價值。