2

Why is it that the url in example one works with $.get, but the URL in example two doesn't? Is there a way to make example two work?

Example One

$.get("http://localhost/magic/pages/tcgqueryoutput.php",
    function(data, statusTxt){
       $('#tcg_query_output').html(data);
    });

Example Two

$.get("tcgqueryoutput.php",
    function(data, statusTxt){
       $('#tcg_query_output').html(data);
    });
3
  • 1
    Check your http logs to see what path the second example resolving to. Commented Dec 27, 2012 at 0:54
  • can't answer without knowing what directory request is being made from, or if you have any MOD Rewrite in place, or Meta base in page . Check in browser console to see what URL is being used for actual request also Commented Dec 27, 2012 at 0:57
  • The file with the $.get script is in the same directory as the request file. Matthew has the correct answer here: I needed to specify relative path a bit more clearly - "/magic/pages/tcgqueryoutput.php". What I don't understand is why using the file string works in online examples of .$get. Commented Dec 27, 2012 at 1:09

2 Answers 2

2

Example two is based off of the current base url. So that working is entirely dependent upon the url routing/directory structure of your web application.

Edit:

In this case, I would imagine it is trying to default to the following route (assuming the route is being targeted from your root directory):

http://localhost/tcgqueryoutput.php

So you need to specify more in the relative url in order for it to resolve correctly.

Sign up to request clarification or add additional context in comments.

2 Comments

The path "/magic/pages/tcgqueryoutput.php" was required. Still don't have an complete answer as to why the single file string doesn't work, but I guess I will need to live with it.
So the reason why you can't do a single file URL like what you supplied is simply because your web server has no idea where that page exists. How is it going to know that in the magic/pages/ directory, there resides a file named tcgqueryoutput.php that your client browser is trying to request??? Answer, it can only do it based against what it does know, the root directory of your application. So if you supply the sub directory path as part of the URL, it can figure out the rest. Make sense?
0

In the first example, you use an explicit path name. It should be best practice that in production you use an explicit path name from the root or a fully qualified path. It seems to me that the reason your second approach does not work is because it is inferring the path name from the wrong place. It will take the current locale, and then append the url you have. There are a couple possibilities to changing this. Usually what happens is you will have the server pass in a path.

var serverPath = /*get value from server*/;

However, you may also just choose to type in this value explicitly,

var serverPath = "http://mydomain.com/";

which will allow you to use that as a base:

$.get(serverPath + "magic/pages/tcgqueryoutput.php",
function(data, statusTxt){
   $('#tcg_query_output').html(data);
});

If you had passed in the serverPath from your server, then you would see this work on both localhost and in production. If you do this manually, you would need to change the value from localhost to mydomain.com.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.