2013-05-06 88 views
0

我收到彈出的Undefined,並顯示如下錯誤性質「DOMAIN_NAME」:的JavaScript運行時錯誤:無法獲取的未定義或空引用

Unable to get property 'domain_name' of undefined or null reference.

可以在此

任何機構幫助

從查看:

@Html.ActionLink("Details", "", 
    new { id = item.id }, 
    new { onclick = "someFunction(" + item.id + ")", 
    href = "javascript:void(0)" }) 

的Javascript

function someFunction(id) { 

    $.ajax({ 
    type: 'POST', 
    url: '@Url.Content("~/")Contracts/Test/', 
    data: { 'id': id },  
    dataType: 'json', 
    success: function (data) { 
     alert(data.domain_name); 
    }, 
    error: function (xhr, status, exception) { 
      alert("Error: " + exception + ", Status: " + status); 
     } 
});  
} 

控制器動作

enter code here 
public JsonResult Test(int id) 
    { 
     var result = (from cntrct in db.contracts where cntrct.id == id 
       select new { cntrct.domain_name, cntrct.id}).ToArray();  
     return Json(result); 
    } 

下面是我「米分貝越來越但是不能傳遞給JavaScript回數據。 enter image description here

錯誤 enter image description here

SCRIPT7002:XMLHttpRequest的:網絡錯誤0x2ef3,無法完成因錯誤00002ef3操作。 合同

+1

所以錯誤是因爲從Json ActionResult傳回的數據爲空;你可以驗證該ID是否正確傳遞給'someFunction',並且具有相應的id值的合約存在於數據庫中? – kieran 2013-05-06 08:11:23

+0

感謝您的快速響應。使用該ID值存在於數據庫中,但無法將該數據傳回至JavaScript .. – Mukarram 2013-05-06 08:17:23

+0

HTML的ID不應僅爲數字。 – Shikiryu 2013-05-06 09:27:42

回答

-2

Oohoo!最後我得到了這個......我們需要做的只是alert(data [0] .domain_name); :)

+0

這並沒有提供問題的答案。要批評或要求作者澄清,在他們的帖子下留下評論 - 你可以隨時評論你自己的帖子,一旦你有足夠的[聲譽](http://stackoverflow.com/faq#reputation),你將能夠[評論任何帖子](http://stackoverflow.com/privileges/comment)。 – slfan 2013-05-06 16:24:57

0

你可以試試這個在JSON操作方法

行動

public JsonResult Test(int id) 
{ 
    var result = (from cntrct in db.Contracts where cntrct.ID == id 
      select new { cntrct.domain_name, cntrct.id, cntrct......}).ToArray();  
    return Json(result); 
} 

腳本

function someFunction(id) { 
    $.ajax({ 
     type: 'POST', 
     url: '@Url.Content("~/")Contracts/Test/', 
     data: { 'id': id }, 
     dataType: 'json', 
     success: function (data) { 
       alert(data.domain_name); 
     } 
    }); 
} 
+0

domain_name在Json數據中有..我可以在調試時看到它 – Mukarram 2013-05-06 08:25:49

+0

我可以通過這個單一的屬性,但無法從控制器傳遞對象到JavaScript ... – Mukarram 2013-05-06 08:46:52

+0

請參考我的答案,我已經編輯它。 – Nemo 2013-05-06 08:48:20

相關問題