简体   繁体   中英

Jquery call to Controller of MVC: ReferenceError: url is not defined

I tried all my efforts, but just can't understand where the error lies. I searched the google also but did not find any good solution. It is not actually calling the controller/action in mvc. The same is running good in the other parts of the project.

I have a contrller "RB" under a folder "MVC", the action is defined as "SS". and I am firing following code from my javascript file :

var sSch = function (request, response) {
        var t = request.RF.substring(0, 1);
        var d = new Date(request.RNR);            
        $.ajax({
            url: "/MVC/RB/SS",
            type: "POST",
            dataType: "json",
            data: {
                _rId: request.ReportId,
                _date: d.toString(),
                _fcy: t
            },
            success: function (data) {
                alert('Success');

            },
            error:  function (data) {
                alert('Error');

            }
        });
    }; 

I am calling this function onClick of a button and properly getting the values in Request variable, but it is not anyhow calling the Controller/Action there. On firebug I tested it throws the exception "ReferenceError: url is not defined". I am using MVC3 under VS 2010.

Please Help.

You have to define your action properly instead of

 url: "/MVC/RB/SS",  

use

 url:  @Url.Action("SS", "RB")

For the url you have 'MVC/RB/SS' this is relative to your current directory a quick test would be to put in url: "../MVC/RB/SS" or url: "../../MVC/RB/SS" depending on how deep you page is in the site structure.

Another way would be to try this:

 url: "@Action("/MVC/RB/SS")",

this will create the correct url at the correct level for you and should be picked up.

Try this:

url: "~/MVC/RB/SS",  

This will resolve to path "http://site-name/MVC/RB/SS".

What does Firebug say?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM