Using Jquery AJAX in seperate Script File?

Posted on 2014-08-07
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
    LVL 21

    Assisted Solution

    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

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

    Expert Comment

    by:Miguel Oz
    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.

    Author Comment

    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 35

    Accepted Solution

    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

    Excellent, thx both...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
    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 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)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now