1

My calls to $.post are not working all over my code. I'm not sending the request to other domains and, actually, I'm doing everything localhosted. My localhost alias was automatically defined by the Mac OS X 10.8 as ramon.local and I'm requesting from http://ramon.local/linkebuy_0.7/resourceX to http://ramon.local/linkebuy_0.7/resourceY. There are no errors on Chrome's console.

The server side doesn't receive the request and I can check it by accessing directly from the browser (typing the URL).

It's not just one call that is not working, none of them are. They were all working days ago and I'm suspicious that I accidentally changed something on my local settings. What could it be?

Here's an example of what I'm facing:

$.post(
    <<CORRECT URL INSIDE THE DOMAIN>>,
    {},
    function(response) {
        console.log('THIS SHOULD BE PRINTED ON CONSOLE');
        alert('THIS SHOULD BE POPPED UP');
    }
);

I don't get the alert, neither the console message while running the code above. So I tried the following:

$.support.cors = true;
$.ajax({
    url: "http://ramon.local/linkebuy_0.7",
    dataType: "json",
    type: "GET",
    crossDomain: true,
    success: function (data) {
        console.log(data);
    },
    error: function (xhr, status, error) {
        alert(error + " - " + status);
    }
});

I just came with $.support.cors = true; and crossDomain: true to check if it was a cross domain issue. So I was alerted No Transport - error same way as before.

What can I do to solve that?

Thanks in advance.

Ramon K.
  • 3,402
  • 3
  • 20
  • 29
  • 4
    When you say "localhosted", do you mean you're using references to a local web server, or `file://` URLs? If the latter, then Chrome won't let you do it. – Pointy Dec 10 '12 at 16:49
  • 3
    Use either firebug or Fiddler to see if the call is going out to the server. Check that it is going to where you are expecting or if you are getting a 404. Since you are using $.post() you are hiding the failure code. Try reimplementing as $.ajax() and specify a function to handle error. – scrappedcola Dec 10 '12 at 16:52
  • Also note that the host and port number have to be **exactly** the same; you can't use "localhost" sometimes and "your-machine-name" other times. – Pointy Dec 10 '12 at 16:54
  • Check the server script to make sure the call is being received. – Barmar Dec 10 '12 at 16:55
  • @Pointy The problem is the host, I guess, because I'm getting the AJAX error `No transport`. I'm using `ramon.local` wich Mac OSX 10.8 has set automatically for me. The page is accessed via `ramon.local` and I'm sending requests to the same domain, so, what should I do? – Ramon K. Dec 10 '12 at 17:43
  • This may sound stupid, but should the `url` be `'http://ramon.local/linkebuy_0.7'` (note the `'http://'`)? – gen_Eric Dec 10 '12 at 18:03
  • @RocketHazmat Just tried again after fixing it. No deal. :( – Ramon K. Dec 10 '12 at 18:30
  • @ramaismon: Darn. Worth a shot. – gen_Eric Dec 10 '12 at 18:31

2 Answers2

1

Try this and see if you are getting any alert:

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post("your url", function() {
    alert("success");
}).success(function() {
    alert("second success");
}).error(function() {
    alert("error");
}).complete(function() {
    alert("complete");
});

// perform other work here ...
// Set another completion function for the request above
jqxhr.complete(function() {
    alert("second complete");
});​
palaѕн
  • 72,112
  • 17
  • 116
  • 136
  • Im getting `Uncaught SyntaxError: Unexpected token ILLEGAL` on the last line. No alerts though. – Ramon K. Dec 10 '12 at 17:38
  • Worked. I got `error` and `complete`. What can I do to check the status of the error? – Ramon K. Dec 10 '12 at 18:04
  • @ramaismon: Try to use [`ajaxError`](http://api.jquery.com/ajaxError/). Look at the `thownError` parameter passed to the callback. – gen_Eric Dec 10 '12 at 18:09
  • 1
    modify the error function `.error(function (request, status, error) { alert(request.responseText); })` – palaѕн Dec 10 '12 at 18:11
  • Got `Object {readyState: 0, status: 0, statusText: "No Transport"}`. Also, I tried to forse CORS with `$.support.cors = true;` but it still doesn't work. – Ramon K. Dec 10 '12 at 18:18
  • If this is indeed the problem (and I suspect it is), you might want to check out JSONP as a solution. Here are a few links that might help you get started: **1.** http://www.west-wind.com/weblog/posts/2007/Jul/04/JSONP-for-crosssite-Callbacks **2.** http://www.jquery4u.com/json/jsonp-examples/ – palaѕн Dec 10 '12 at 18:40
  • @PalashMondal Well, thanks for the links. But... how is it possible? I'm requesting from `localhost` to `localhost` and it was working before. It stopped working but I didn't changed anything from the working code. – Ramon K. Dec 10 '12 at 19:03
1

Well, I solved the problem in a very strange way.

I deleted the JQuery file and downloaded it again, replacing the old one. Happens it worked out.

So, if you're:

  • Making AJAX requests that are not cross-domain;
  • Using JQuery for it (e.g. $.post, $.get, etc);
  • Getting No Transport AJAX error

Then re-download and replace you're JQuery source.

Else, if you're making cross-domain requests (not this case), then look for JSONP and try to set $.support.cors = true; at the beginning of you're code.

Thanks everyone for the comments and answers.

Ramon K.
  • 3,402
  • 3
  • 20
  • 29