Using Jquery AJAX in seperate Script File?

Posted on 2014-08-07
Medium Priority
Last Modified: 2014-08-11
Since I moved my scripts from the MVC view into separate .js file, it cannot connect to another controller Action anymore.

Q. Can a AJAX work correctly in a separate .js file?
Question by:WorknHardr
  • 3
  • 2
LVL 21

Assisted Solution

by:Craig Wagner
Craig Wagner earned 700 total points
ID: 40248970
Yes it can, we do it all the time.

Open the JavaScript console in your browser or use Fiddler to look at the call to try to determine what is wrong.

Author Comment

ID: 40250676
In some cases I have a partial view inside another partial view. I thought maybe the Ajax URL might need to be changed.
LVL 36

Expert Comment

by:Miguel Oz
ID: 40251235
The js file may be in a different location from where it was originally run, thus you need to change the URL parameter of your ajax call.
For example if you use jquery ajax, implement the error method and post the error message.  As stated previously by CraigWagner, Fiddler can help to capture  the call interaction and find the error.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Author Comment

ID: 40251616
I've found many solutions where adding / solved their issue like so:    
instead of:

I'm curious if this would work:
   var URL = document.location
LVL 36

Accepted Solution

Miguel Oz earned 700 total points
ID: 40252919
Yep, you just confirmed that the URL was the issue. The extra "/" at the start of the URL makes it a relative URL.
Regarding post question "URL/controller/action " will only work if it resolves in a valid location in your server. This is known as an absolute URL.
For more info about URLs check:

Author Closing Comment

ID: 40254670
Excellent, thx both...

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question