0

It is not first time I use $.get function, but this time I dont know why it is not working

function test(tid)
  {
    alert("1");
   var ttt;
   alert("2");
   var ddd;
    alert("3");

   $.get("demo_ajax_load.txt", function(result){
    alert("4");
     });
   }

demo_ajax_load.txt is just a text file content is: Welcome

I am getting alert 1,2,3 but it is not showing 4th alert message. everything looks normal here, what is wrong with this simple function?

Thanks

12
  • Are there any errors in the console? Commented Jul 4, 2012 at 21:38
  • Open the Network tab in Chrome, check the demo_ajax_load.txt request and give us the status. Commented Jul 4, 2012 at 21:38
  • 2
    api.jquery.com/jQuery.get The 4th alert will only occur if the ajax call succeeded. Apparently it does not. Commented Jul 4, 2012 at 21:39
  • Make sure that the file demo_ajax_load.txt has the right path Commented Jul 4, 2012 at 21:41
  • I checked Network Tab, but couldnt find anything related to demo_ajax_load.txt Commented Jul 4, 2012 at 21:44

2 Answers 2

1

It will probably have nothing wrong as long as you are not testing it locally.

if you're using Google Chrome (I'm using version 20), open the console (press F12) and you will see that you will get an error:

Origin null is not allowed by Access-Control-Allow-Origin.

enter image description here

in Mozilla Firefox, you will get

[object XMLDocument]

but if you do this remotely (in your host), everything will get working normally...

you can see my Live DEMO and open the console to see the output if you like.

my simple jQuery:

$.when(
    $.get("a.txt")
  )
 .done(function(data) { 
    console.log("All done: " + data); 
    $(".txt-from-file").text(data); 
  })
 .fail(function(data) { 
    console.log("Error found: " + data.statusText); 
    $(".txt-from-file").text(data.statusText); 
  });

or you can always simplify more and use:

$.get("a.txt").then(
    function() { alert("succeeded"); },
    function() { alert("failed!"); }
);
Sign up to request clarification or add additional context in comments.

4 Comments

I have aded console.log commands, tryed it on chrome, pressed F12, it showed me js script code, I didnt get any error
and I am doing it on server, I am on dedicated web server
Sorry , wait, let me look at console, I mis looked before
I am having bunch of errors : 1- cant open css not important I think 2- mischandler is not defined index.php:735 3-Failed to load resource: the server responded with a status of 404 (Not Found) swfobject_modified.js Uncaught TypeError: Object function $(element) { if (arguments.length > 1) { for (var i = 0, elements = [], length = arguments.length; i < length; i++) elements.push($(arguments[i])); return elements; } if (Object.isString(element)) element = document.getElementById(element); return Element.extend(element); } has no method 'get' countdown.js:7
0

Ok Thanks for trying to help me , I found solution, I should use jQuery instead of $ , and it worked

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.