2013-05-06 92 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

相关问题