Link to home
Start Free TrialLog in
Avatar of Niall Gallagher
Niall GallagherFlag for Ireland

asked on

Javascript runtime error: Function Expected"

I put this bit of code in and I have to admit I'm not the best at JS but I cannot see what is causing this error.
I am getting "Javascript runtime error: Function Expected"
Any help is greatly appreciated.

Here is my code:
function Search() {
        'use strict';
        $.ajax({
            url: 'Account/AccountController'(strName, strTelephone, strNum, strStreet, strApt, strZip, strIP),
            type: 'POST',
            datatype: 'json',
            cache: false,
            data: { strname: strName, strtelephone: strTelephone, strnum: strNum, strStreet: strStreet, strapt: strApt, strzip: strZip, strip: strIP },
            success: function $(status) {
                alert("Success");
            },
            error: function $(status, err) {
                alert(status + "," + err);
            }
        });
    };

Open in new window

Avatar of Bill Prew
Bill Prew

I don't think you want the extra stuff on the URL line...

url: 'Account/AccountController'(strName, strTelephone, strNum, strStreet, strApt, strZip, strIP),


»bp
Avatar of Niall Gallagher

ASKER

Hi Bill, and thank you for your response, but when I took out the guts and left

function Search() {
        'use strict';
        $.ajax({
            url: 'Account/AccountController'(strName, strTelephone, strNum, strStreet, strApt, strZip, strIP)
})
};

I still got the same error
I only meant this stuff on the actual URL line, what's that about?

(strName, strTelephone, strNum, strStreet, strApt, strZip, strIP)


»bp
I think I already tried that but I will try again, that is the supposed to be the parameters being passed to the function
I tried this, this time and same response

  function Search() {
        'use strict'
        $.ajax({
            url: 'Account/AccountController'(),
            type: 'POST',
            datatype: 'json',
            cache: false,
            data: { strname: strName, strtelephone: strTelephone, strnum: strNum, strStreet: strStreet, strapt: strApt, strzip: strZip, strip: strIP },
            success: function $(status) {
                alert("Success");
            },
            error: function $(status, err) {
                alert(status + "," + err);
            }
        });
    };
You don't want the empty parens at the end of the URL line either:

()


»bp
OK, Bill,
Things are moving slightly, when I digested what you said I thought I would try it by removing the () as well, and when I removed them it errored out but at least it tried to run the function, now I have to figure out what caused the error
It didn't say much just my  Error pop up saying [ object Object], error
Okay, keep chipping away...


»bp
Try changing the line
alert(status + "," + err);

Open in new window

to the following:
console.log(status, err);

Open in new window

and check in the console.  Then you'll be able to see what's wrong
Can you give us more context. The function as it stands is fine - providing the URL resolves and the variables are available.
Thanks Julian
This is the Controller, Action, I am trying to call

public class AccountController : Controller
    {      
        private ApplicationUserManager _userManager;
 
        public AccountController(string strName, string strTelephone, string strnum, string strStreet, string strApt, string strZip, string strIP)
        {
            FaultRepository FR = new FaultRepository();
            var data = FR.getConn(Environment.UserDomainName + "/" + Environment.UserName, strName, strTelephone, strnum, strStreet, strApt, strZip, strIP);
           
        }

I'm praying you can see a silly mistake I've made
Ok but the error is in the JavaScript portion of your code.

You gave us some JavaScript and an error but you have not told us what that error is referring to - we need to see the offending line.

Can you tell us where the error is being reported (line number) of the code you posted in your OP.
Looking at your original $.ajax() call, there are several things wrong:
1. You don't pass POST variables in the URL; those get appended to body by the $.ajax function when correctly configured.
2. You are posting to a relative URL; best practice is to make that absolute either with a fully qualified URL or by starting from '/'.
3. You are currently trying to post to your AccountController's constructor... which won't work.
4. Your success and error functions have an extra $ character preceding them; this will confuse the jQuery interpreter and the JavaScript engine in general.

At a minimum, I would modify your client-side JS $.ajax() call as follows:
function Search() {
        'use strict';
        $.ajax({
            url: '/Account/AccountController',
            type: 'POST',
            datatype: 'json',
            cache: false,
            data: { strName: strName, strTelephone: strTelephone, strnum: strNum, strStreet: strStreet, strApt: strApt, strZip: strZip, strIP: strIP },
            success: function (status) {
                alert("Success");
            },
            error: function (status, err) {
                alert(status + "," + err);
            }
        });
    }; 

Open in new window


Then -- at a minimum -- I would update your AccountController as follows:
public class AccountController : Controller
{       
        private ApplicationUserManager _userManager;
 
        public AccountController(ApplicationUserManager userManager)
        {
            _userManager = userManager ?? throw new ArgumentNullException(nameof(userManager));
        }
 
        public ActionResult Post(string strName, string strTelephone, string strnum, string strStreet, string strApt, string strZip, string strIP)
        {
            FaultRepository FR = new FaultRepository();
            var data = FR.getConn(Environment.UserDomainName + "/" + Environment.UserName, strName, strTelephone, strnum, strStreet, strApt, strZip, strIP);
            
        }
}

Open in new window


That should get you going in the right direction. Note that I've made some assumptions about your application that may or may not be accurate... such as you're using Dependency Injection, you're using the current version of ASP.NET MVC, etc.

Finally, it would be better to post a single object with Properties rather than a set of literal parameters. The MVC Model Binder is very finicky about dealing with primitives and can cause a lot of grief that simply stuffing those parameters into an object as Properties solves very easily.
Kelvin,
Thank you I will definitely take a look at this and hopefully get a chance to try it before the weekend. I apologize for taking a while to get back but I got married last week and wanted to take sometime away from the computer.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.