I have a file with the name foo.htm inside a folder abc. I am invoking the below script from another html page known as query.html in the folder abc.

    $.ajax({
           url: '/foo.htm',
           method: 'GET',
           complete: function(a,b){
                 alert('complete-2');
           },
           success: function(a){
                  alert('good-2');
            },
            error: function(a,b){
                   alert('problem-2'); 
            },
    });

However, I am getting the message complete-2 and problem-2. It doesn't show the message good-2. What could be
the problem?.

Your help is kindly appreciated.

Recommended Answers

All 6 Replies

Path navigation:

  • / : site root
  • ./ : same directory (optional)
  • ../ : parent directory
  • ../../ : grandparent directory
  • X/ or ./X/ : down into directory X

Everything except / works relative to the current page.

Therefore, try :

url: 'foo.htm',

or

url: './foo.htm',

I still get the same message which is problem-2 and complete-2.

    $.ajax({
           url: './foo.htm',
           method: 'GET',
           success: function(a){
                  alert('good-2');
            },
            error: function(a,b){
                   alert('problem-2'); 
            },           
            complete: function(a,b){
                   alert('complete-2');
           },
    });

The path should be relative to the file the JavaScript code is stored in. If that's not query.html, you'll likely get undesirable results.

Try making the path absolute from your webroot. E.g. url: '/abc/foo.htm'
(guessing at file structure)

Blocblue,

The path should be relative to the file the JavaScript code is stored in. If that's not query.html, you'll likely get undesirable results.

Sorry, but that is not correct. The javascript environment follows the same rule as the web page in which it runs - ie relative to the page's location, not where the javascript was served from, which could theoretically be anywhere on the web.

External CSS style sheets are diffferent. URLs in style directives (eg background-image:url(...)) do work relative to the style sheet's location. Maybe that's what you were thinking of.

/abc/foo.htm may work, but only if the site is properly served under the http:// protocol, not under file://.

Soloman,

Try :

error: function(jqXHR, textStatus, errorThrown) {
    alert('problem-2: ' + textStatus + ', ' + errorThrown); 
}   

This will give you more of a clue as to what type of error has occurred.

commented: I was thinking of CSS urls, and assumed the same was true for both. I've never really given it that much thought. Thanks for the correction. +0

The error thrown is permission denied.

For some reaon the server is not allowing foo.htm to be severd.

You could try a differnet file name and/or a different extension.

It's unlikely but the server could be configured to reject ajax requests, in which case nothing clent-side will fix it. The server configuration would need to be changed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.