Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a code:
XML
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.
Posted

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
 
Share this answer
 
don't have any problem in
/ShoppingCart/' + pid + '/getDetailsProduct'


because i use route
"{controller}/{id}/{action}"


when i'm at index page, Json return "trung" but in other page, it return undefined.


Now, i know how to fix it.


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


if not in index page, i use : data = JSON.parse(data) when result return.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900