2015-08-13 132 views
0

POST操作我有與所述柱操作時出現問題,接收空對象 我有這樣的JavaScript代碼來調用動作 將數據傳遞到通過Ajax

function forwardMsg(result) { 
 
     
 
      $.ajax({ 
 
       type: 'POST', 
 
       url: '@Url.Action("ForwardHCFASuspendedMessage")', 
 
       dataType: 'json', 
 
       data:JSON.stringify({ 
 
        messageId: "@Model.MessageId", 
 
        result: result, 
 
        serviceLines: "@Model.ServiceLineSegment" 
 
       }), 
 

 
       error: function (response) { 
 
        alert(response.responseText); 
 
       }, 
 
       success: function (response) { 
 
        alert(response); 
 
       } 
 

 
      }); 
 
     }
,並接收一個空對象如圖此圖像中 enter image description here

+0

$(function() { function forwardMsg() { var dataToSend = JSON.stringify({ Id: @Model.Id, SomeData: '@Model.SomeData', Result: "Another test data" }); $.ajax({ type: 'POST', url: '@Url.Action("ForwardHCFASuspendedMessage", "Home")', dataType: 'json', contentType: 'application/json', data: dataToSend, error: function (response) { alert(response.responseText); }, success: function (response) { alert(response); } }); } $('body').on('click', '#myBtn', forwardMsg); }); 

它開始爲我的作品後加入數據,儘量參數名 '身份證',而不是 '數據' 在你的功能,如: '公衆的ActionResult ForwardHCFASuspendedMessage(ForwardMessageModel ID)' – Marcin

+0

does not work too – Hana

+0

如果您使用javascript發送模型,則必須接收模型變量作爲控制器中的參數;如果您發送的ID,serviceLines,不管,你必須得到適當的數據類型 – chiapa

回答

0
使用jQuery的 $post()

var data = { 
    messageId: "@Model.MessageId", 
    result: result, 
    serviceLines: "@Model.ServiceLineSegment" 
}; 

$.post('@Url.Action("ForwardHCFASuspendedMessage")', JSON.stringify(data)) 
.success(function (response) { 
    alert(response); 
}) 
.fail(function (response) { 
    alert(response.responseText); 
}); 

這可能需要車

嘗試設置和ajax調用將JSON數據發佈到端點。這意味着所有的努力都是爲你完成的,你需要提供的是網址和數據。

編輯:

嘗試呼叫,而不serviceLines: "@Model.ServiceLineSegment"看看會發生什麼。您需要在轉換成JavaScript陣列,使得POST調用之前,因爲看到:

System.Collections.Generic .List 1 XIP.Infrastructure.Data.Claims.ServiceLineSegment_HCFA]「`

請求體

是不是你。希望我想你可以這樣做:

​​
+0

我看到瀏覽器網絡響應後發送的數據,我發現它已經發送像這樣「{」messageId「:」e2911271-c0a1-4f5e-9773-39e669c576d8 「,」結果「:1,」serviceLines「:」System.Collections.Generic .List'1 [XIP.Infrastructure.Data.Claims.ServiceLineSegment_HCFA]「}」但不知道爲什麼它沒有收到動作 – Hana

+0

是它在路由配置中存在問題?感謝, – Hana

+0

將嘗試此更新 – Hana

0

function forwardMsg(result) { 
 
var obj = {messageId: "@Model.MessageId", 
 
        result: result, 
 
        serviceLines: "@Model.ServiceLineSegment"}; 
 
     var jsonStringHere = JSON.stringify(obj); 
 

 
      $.ajax({ 
 
       type: 'POST', 
 
       url: '@Url?action=ForwardHCFASuspendedMessage', 
 
       dataType: 'json', 
 
       data: {jsonStringReceiver:jsonStringHere}, 
 
       error: function (response) { 
 
        alert(response.responseText); 
 
       }, 
 
       success: function (response) { 
 
        alert(response); 
 
       } 
 

 
      }); 
 
     };

0

看THI S:

enter image description here

我在CSHTML代碼:如果你沒有用第一元素名稱正確路由

contentType: 'application/json',