0

I have the following AJAX:

$.ajax({
          url: url,
          data: {"url" : hangOutUrl, "participants" : params},
          dataType: 'jsonp',
          success: function(){
            console.log("Connected");
          },
          error: function(xhr, textStatus, error) {
          console.log("Impossible to connect");
          console.log(xhr.statusText);
          console.log(textStatus);
          console.log(error);
      }
        });

And this is code on my controller

def hangout_started

    #Get information from hangout using ajax and widget
    url = params[:url]
    participants = params[:participants].split(/-/)


    #Find users 
    caller1 = Kid.where(username: participants[0]).first
    caller2 = Kid.where(username: participants[1]).first

    #Set all users to busy
    caller1.set_busy_status!

    #Create REDIS row with user and URL 

    REDIS.sadd "hangout:#{caller2.id.to_s}", url
    REDIS.expire "hangout:#{caller2.id.to_s}", 60

    respond_to do |format|
        format.json { head :ok }
      end


  end

And this work perfect, except that I always get "Impossible to connect" on my console log. Why I always enter into my error function?

Error function and post error from browser console:

message: "jQuery211019731531548313797_1401196032110 was not called" stack: "Error: jQuery211019731531548313797_1401196032110 was not called↵ at Function.n.extend.error (https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js:2:1821)↵ at h.jsonp.b.dataTypes.(anonymous function).b.converters.script json (https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js:4:16293)↵ at vc (https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js:4:7397)↵ at x (https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js:4:10802)↵ at HTMLScriptElement.n.prop.on.c (https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js:4:15583)↵ at HTMLScriptElement.n.event.dispatch (https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js:3:6404)↵ at HTMLScriptElement.r.handle (https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js:3:3179)"

halfer
  • 19,824
  • 17
  • 99
  • 186
Jean
  • 5,201
  • 11
  • 51
  • 87
  • 1
    Hard to say without more info. Log the arguments to the `error` function. – Biffen May 27 '14 at 10:49
  • there is no error, it just enter to my error: function(){ console.log("Impossible to connect"); } but no errors and everything works great – Jean May 27 '14 at 10:52
  • Still though, something like `error: function(jqXHR jqXHR, String textStatus, String errorThrown) { console.log("Impossible to connect: " + textStatus + "; " + errorThrown); }` might help you find the culprit. – Biffen May 27 '14 at 10:54
  • 1
    I getting this error: hangout-blabloo.js:37 Error message: "jQuery21107414210103452206_1401189257306 was not called" stack: "Error: jQuery21107414210103452206_1401189257306 was not – Jean May 27 '14 at 11:15

1 Answers1

2

Ajax

Something you need to know about $.ajax - when you use the error callback, it's caused by your ajax hitting an actual error. I used to think it was if your app returns an error - that's wrong:

  • success: {} is for every time your ajax hits the URL & delivers the request
  • error: {} is for every time your ajax fails to hit the URL correctly

This means if the error was in your controller, I believe it will come back with a success callback from your controller


Fix

For you, I would recommend this:

  1. Determine your ajax is hitting the right url (you've not detailed url in your code)
  2. Be sure you've included jquery in your javascript application js
  3. Use a way to capture the returned error from your ajax
  4. Try using json

You may wish to change your ajax to this:

$.ajax({
     url: url,
     data: {"url" : hangOutUrl, "participants" : params},
     dataType: 'json',
     success: function(){
        console.log("Connected");
     },
     error: function(jqXHR,error, errorThrown) {  
           if(jqXHR.status&&jqXHR.status==400){
                alert(jqXHR.responseText); 
           }else{
               alert("Something went wrong");
           }
      }
});

This will allow you to capture the error response properly, giving you the ability to show the error correctly. You can do that by looking at this:


If you give me something to work on, I'll be in a much better position to help you out!

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • Hello Rich, thanks for your post. Well, I'll try to explain what I want to do. This is a Google Hangout Widget, when the widget start, I sent the hangout url to my app, in order con show the user2 that someone is calling him. The ajax code call my /hangout_started url, and then execute the action of my controller and this works great, but when I check the console I find always the message of my error function of my ajax call. I'm going to update the code with the error function and the error. – Jean May 27 '14 at 13:06
  • Thanks Jean! I've ever used the `hangout widget`, so I hope I can digest your updated response. Either way, I want to help you with this! – Richard Peck May 27 '14 at 13:13
  • Oh I forgot to tell you that I'm using jsonp beacuse that was the only way I found in order make this work, because when I use json, I always get XMLHttpRequest cannot load bla bla bla NO 'Access-Control-Allow-Origin', I tried many codes to make CORS works, but the only option that works for me was using jsonp. – Jean May 27 '14 at 13:24
  • [CORS](http://enable-cors.org/) problems... are you connecting with an external server? If so, can you change the CORS policy to allow your domain? – Richard Peck May 27 '14 at 13:28
  • Yes, this code is executed from a google hangout widget (with the hangout url) to my app on our server (with our domain). – Jean May 27 '14 at 13:32
  • Okay that's cool! Do you have the ability to permit connectivity to your domain from your hangout account or something/ – Richard Peck May 27 '14 at 13:34
  • Rich, thanks for you help, I found that the problems is that my action on my controller is returning an HTML, but I change the render format to .to_json, but I still getting the html file, I don't understand why. Any idea? – Jean May 28 '14 at 11:19
  • Could be that you're sending a non-json request. Have you tried `format.js` to see? – Richard Peck May 28 '14 at 12:34
  • 1
    I solved, I didn't have a json view :s and that solve my problem. Thanks – Jean May 28 '14 at 12:52