0

I'm not the most knowledgeable when it comes to AJAX so I'm having a very difficult time debugging what I would think would be a very simple AJAX request. The request sends data to a URL via GET and is supposed to either be returned a 1 or 0. Instead nothing is returned and the error message is blank. The confusing part for me is that I can copy and paste the URL along with the url encoded variables (so just like what it would be if I submitted a form with the GET method) and the remote server will return a 1 or a 0 just fine.

Firebug does not show that any errors were thrown either. All I can see from it was that the request was made, the right params and associated values were passed and that the html is completely blank.

EDIT : This is not browser specific. Tested in Firefox, IE, and Chrome.

Code:

var userData = {
      firstname: $('#firstname').val(),
      lastname: $('#lastname').val(),
      addr1: $('#addr1').val(),
      ct: $('#ct').val(),
      stateprovince: $('#stateprovince').val(),
      zipcode: $('#zipcode').val(),
      primary_phone: $('#primary_phone').val(),
      gradyear: $('#gradyear').val(),
      school_id: $('#school_id').val(),
      source_id: $('#source_id').val()
}

$.ajax({
    type: "GET",
    url: "http://blahblahblah.com/blah",
    data: userData,
    success: function() {
        alert("it worked!");
    },
    error: function(xhr,err){
        alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
        alert("responseText: "+xhr.responseText);
    }
});

The error alerts return:

readyState: 0
status: 0

and

repsonseText:

Any help would be greatly appreciated! I'll respond back as fast as I can if any additional info is requested!

Reasoned
  • 137
  • 2
  • 12
  • 1
    try to print err-- alert(err), you will see the message. – Ramesh Kotha Feb 13 '14 at 21:12
  • possible duplicate of [Jquery ajax random error in Chrome only](http://stackoverflow.com/questions/17778504/jquery-ajax-random-error-in-chrome-only) – Valamas Feb 13 '14 at 21:16
  • It just prints "error". – Reasoned Feb 13 '14 at 21:17
  • see my link above. you have all the symptoms of the problem i had. Zero for status codes. 'error' for error msg. – Valamas Feb 13 '14 at 21:18
  • This is in Firefox, I'll try the cache technique – Reasoned Feb 13 '14 at 21:19
  • Tried setting "cache: false". Still has the same problem occurring. – Reasoned Feb 13 '14 at 21:25
  • Well maybe `http://blahblahblah.com/blah` is just not returning the data you _expect_ it to return …? – CBroe Feb 13 '14 at 21:38
  • I think it is. Unless I am not handling the AJAX request properly. I can type in the URL along with the variables as though it were all URL encoded, etc. and it returns a 1 or 0 every time. Whenever I go through AJAX it just don't seem to have any luck. – Reasoned Feb 13 '14 at 21:41
  • 1
    @Reasoned Can you compare with fiddler (for instance) the ajax request and your manual request (meaning pasting the url) ? – Omri Aharon Feb 13 '14 at 21:48
  • Fiddler presents the response as text but says it is encoded and may need to be decoded before inspection. After clicking on an option to decode the response, I am given the "1" value I was looking for. When I do the manual request does my browser automatically decode it before presenting it as plain text? If so, how would I go about decoding it before the callback? – Reasoned Feb 13 '14 at 22:08
  • @Reasoned Would it be possible, just for figuring out the problem purposes, to try to make the ajax a post request and see if you're still receiving an error? – Omri Aharon Feb 13 '14 at 22:37
  • The remote server that is processing the data is not in my control so I can't make the function work using post. The company requires the data to be sent via get. If I switch it to post I receive a 403 forbidden error. Is it possible that my problem is caused by the server sending the data via chunked transfer-encoding? – Reasoned Feb 13 '14 at 23:10
  • @Reasoned Not sure.. By the way, I just noticed you replied to my fiddler comment with the response from fiddler, but did you compare to the very last header that both requests (AJAX and manual browser get) to the server are identical? – Omri Aharon Feb 14 '14 at 07:06
  • Everything seems to be the same for the most part. The only difference was that the request header was accepting more than just text/html in the manual request while the ajax request was only accepting text/html. The response header shows the content type is text/html but I tested for it just in case and added the appropriate code so that the ajax request headers would accept the same content as the manual request and still didn't have any luck. – Reasoned Feb 14 '14 at 14:20
  • @Reasoned I'll try and think what this could be and I'll update if I get any new ideas. – Omri Aharon Feb 14 '14 at 20:16
  • Thanks for the help. I got pulled off on some other programming I need to get done but I'll get back to it on Monday and see if I can't figure it out. – Reasoned Feb 14 '14 at 20:20

1 Answers1

1

The problem was that I was trying to receive text as a response on a cross domain (outside of my server's domain) AJAX call. Apparently browsers disable cross domain javascript calls for security reasons and will only accept a response in the format of "jsonp" and "script".

After I discovered the problem I searched for answers on how to get around this problem. This answer gave me the solution I was looking for and allowed me to obtain the desired results with just a little tweaking to fit my situation.

Thanks for helping me get to the bottom of this!

Community
  • 1
  • 1
Reasoned
  • 137
  • 2
  • 12