我的MVC没有收到Json返回值 [英] My MVC does not receive Json return value

查看:110
本文介绍了我的MVC没有收到Json返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码:

公共JsonResult getDetailsProduct(字符串ID)
       {
           返回this.Json(new {detPro ="trung"});
       } 



和Json函数:

function showPMuaHang(pid) {
    var url = ''/ShoppingCart/'' + pid + ''/getDetailsProduct'';
    $.ajax({ type: "POST", url: url,
        success: function (data) {
            document.getElementById("popup_muahang").innerHTML = data.detPro;
        }
    })



我使用href呼叫Json:

<a onclick="#" href="javascript:showPMuaHang(''70'')">Mua hàng</a>




如果我在本地.../索引页面中单击"a",Json返回"trung",但是如果我从本地.../ShoppingCart/调用它,Json返回"trung",但它显示未定义".

我正在用MVC编码网站.

解决方案

.ajax({type:"POST",url:url, 成功:功能(数据){ document.getElementById("popup_muahang").innerHTML = data.detPro; } })



我使用href呼叫Json:

<a onclick="#" href="javascript:showPMuaHang(''70'')">Mua hàng</a>




如果我在本地.../索引页面中单击"a",Json返回"trung",但是如果我从本地.../ShoppingCart/调用,Json返回"trung",但它显示未定义".

我正在用MVC对网站进行编码.




请按如下所示修改您的JavaScript函数:

function showPMuaHang(pid) {
           var url = ''/ShoppingCart/getDetailsProduct/'' + pid;


.ajax({ type: "POST", url: url, success: function (data) { document.getElementById("popup_muahang").innerHTML = data.detPro; } }) }



问题出在url-您的路径如下-

/ShoppingCart/'' + pid + ''/getDetailsProduct''


哪里pid-70
但您的动作或方法没有70.在网址"

getDetailsProduct

"中被视为"ID"

以下是正确的格式-

/ShoppingCart/getDetailsProduct/'' + pid

是-

/ShoppingCart/getDetailsProduct/70



它将进入ShoppingCartController并为pid = 70


搜索

getDetailsProduct

I have a code:

public JsonResult getDetailsProduct(string id)
       {
           return this.Json(new { detPro = "trung" });
       }



and Json function:

function showPMuaHang(pid) {
    var url = ''/ShoppingCart/'' + pid + ''/getDetailsProduct'';
    $.ajax({ type: "POST", url: url,
        success: function (data) {
            document.getElementById("popup_muahang").innerHTML = data.detPro;
        }
    })



And i use a href to call Json:

<a onclick="#" href="javascript:showPMuaHang(''70'')">Mua hàng</a>




If i click "a" in local.../Index page, Json return "trung" but if i call it from local.../ShoppingCart/, Json return "trung" but i it show "undefined".

I''m coding a website in MVC.

解决方案

.ajax({ type: "POST", url: url, success: function (data) { document.getElementById("popup_muahang").innerHTML = data.detPro; } })



And i use a href to call Json:

<a onclick="#" href="javascript:showPMuaHang(''70'')">Mua hàng</a>




If i click "a" in local.../Index page, Json return "trung" but if i call it from local.../ShoppingCart/, Json return "trung" but i it show "undefined".

I''m coding a website in MVC.


Hi,

Please modify your javascript function as follows :

function showPMuaHang(pid) {
           var url = ''/ShoppingCart/getDetailsProduct/'' + pid;


.ajax({ type: "POST", url: url, success: function (data) { document.getElementById("popup_muahang").innerHTML = data.detPro; } }) }



problem is with url - your path is as follows -

/ShoppingCart/'' + pid + ''/getDetailsProduct''


where pid - 70
but you dont have 70 as your action or method.In URL ''

getDetailsProduct

'' is treated as " ID "

Following is the right format -

/ShoppingCart/getDetailsProduct/'' + pid

which is -

/ShoppingCart/getDetailsProduct/70



It will goto ShoppingCartController and will serach for

getDetailsProduct

for the pid = 70


这篇关于我的MVC没有收到Json返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆