Author Topic: Gah! JQuery problem.  (Read 4667 times)

Rob

  • New improved. Now with added something...
  • Jackass In Charge
  • Posts: 5959
  • Karma: +86/-149
  • Approaching 60 from the wrong damn direction...
Gah! JQuery problem.
« on: October 16, 2013, 11:24:15 AM »
This is really fucking frustrating me. Can someone do a quick sanity check on this for me please?

Code: [Select]
<script>
    function SrchSubmit() {

        var valu = $("#valu").val();

        var url1 = "";
        url1 += "https://www.blahblahblah";
        url1 += "?act=API_DoQuery";
        url1 += "&query={56.EX." + valu + "}";
        url1 += "&clist=3";
        url1 += "&options=num-1";

       $.get(url1, function(xml1) {
alert("Success");
                var rid = $("record_id_", xml1).text();
                var url2 = "";
                url2 += "https://www.blahblahblah";
                url2 += "?a=dr";
                url2 += "?rid=" + rid;
                document.location.href = url2;
            });
}
</script>

Everything works fine until the "$.get(url1, function(xml1) {" line, but this section seems to be just stepped over. The url1 is valid because I can grab it in Chrome's development tools (thank you Google!). It feels like a dumb syntax problem, but I just can't see it, and I don't know how to proceed.

SrchSubmit is called from some HTML.

:(

micah

  • A real person, on the Internet.
  • Ass Wipe
  • Posts: 6915
  • Karma: +58/-55
  • Truth cannot contradict truth.
    • micahj.com
Re: Gah! JQuery problem.
« Reply #1 on: October 16, 2013, 11:36:09 AM »
What is the "record_id_"  that you're trying to get the text of? are you trying to append the xml1 value to the end of that value?

should: var rid = $("record_id_", xml1).text();
be: var rid = $("#record_id_" + xml1).text();
?
"I possess a device, in my pocket, that is capable of accessing the entirety of information known to man.  I use it to look at pictures of cats and get in arguments with strangers."

Mike

  • Jackass In Charge
  • Posts: 11248
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: Gah! JQuery problem.
« Reply #2 on: October 16, 2013, 11:42:03 AM »
Are you using a recent version of jQuery (like 1.8+)?  If so try this:
Code: [Select]
$.get(url, function(xml1) {
      // your stuff
}).fail(function() {
    console.log('$.get failed');
});

fail does have some parameters but I couldn't find what they are.

Also helpful to use a proxy like Charlies or Fiddler2 to see what is going on over the wire.

Rob

  • New improved. Now with added something...
  • Jackass In Charge
  • Posts: 5959
  • Karma: +86/-149
  • Approaching 60 from the wrong damn direction...
Re: Gah! JQuery problem.
« Reply #3 on: October 16, 2013, 11:45:36 AM »
What is the "record_id_"  that you're trying to get the text of? are you trying to append the xml1 value to the end of that value?

should: var rid = $("record_id_", xml1).text();
be: var rid = $("#record_id_" + xml1).text();
?


It doesn't even get that far sadly. I never even hit the "alert("Success");".

Rob

  • New improved. Now with added something...
  • Jackass In Charge
  • Posts: 5959
  • Karma: +86/-149
  • Approaching 60 from the wrong damn direction...
Re: Gah! JQuery problem.
« Reply #4 on: October 16, 2013, 11:45:54 AM »
Are you using a recent version of jQuery (like 1.8+)?  If so try this:
Code: [Select]
$.get(url, function(xml1) {
      // your stuff
}).fail(function() {
    console.log('$.get failed');
});

fail does have some parameters but I couldn't find what they are.

Also helpful to use a proxy like Charlies or Fiddler2 to see what is going on over the wire.

Thanks Mike. Off to try it now.

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14305
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: Gah! JQuery problem.
« Reply #5 on: October 16, 2013, 11:50:00 AM »
I actually prefer the 'Net' tab within Firebug for watching the http requests.  Chrome's dev tools have something similar too.

Mike

  • Jackass In Charge
  • Posts: 11248
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: Gah! JQuery problem.
« Reply #6 on: October 16, 2013, 11:54:02 AM »
What is the "record_id_"  that you're trying to get the text of? are you trying to append the xml1 value to the end of that value?

should: var rid = $("record_id_", xml1).text();
be: var rid = $("#record_id_" + xml1).text();
?


It doesn't even get that far sadly. I never even hit the "alert("Success");".
Been there, done that.  Waaayyyy more often than I'd like to admit.

I actually prefer the 'Net' tab within Firebug for watching the http requests.  Chrome's dev tools have something similar too.
Yeah, both have in browser network tools.  The problem I've found with chrome's is that redirects clears the log.  Plus, I often have to proxy non-browsers (like my phone) so I've just gotten used to using an external proxy.

Rob

  • New improved. Now with added something...
  • Jackass In Charge
  • Posts: 5959
  • Karma: +86/-149
  • Approaching 60 from the wrong damn direction...
Re: Gah! JQuery problem.
« Reply #7 on: October 16, 2013, 12:09:10 PM »
Hmm. Whaddya know? As soon as I add that function, it works fine. Makes me even more suspicious that I have a punctuation error in the original.

:suspicious:

Thanks guys.

Mike

  • Jackass In Charge
  • Posts: 11248
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: Gah! JQuery problem.
« Reply #8 on: October 16, 2013, 12:12:41 PM »
I'd add an alert after the get in the original then and see if it fires off.  Also, check the console as syntax problems will be reported there in chrome (and I assume FF)

Rob

  • New improved. Now with added something...
  • Jackass In Charge
  • Posts: 5959
  • Karma: +86/-149
  • Approaching 60 from the wrong damn direction...
Re: Gah! JQuery problem.
« Reply #9 on: October 16, 2013, 12:23:32 PM »
What is the "record_id_"  that you're trying to get the text of? are you trying to append the xml1 value to the end of that value?

should: var rid = $("record_id_", xml1).text();
be: var rid = $("#record_id_" + xml1).text();
?


Just checking this Micah. The XML returned is

Code: [Select]
<qdbapi>
<action>API_DoQuery</action>
<errcode>0</errcode>
<errtext>No error</errtext>
<dbinfo>
<name>Parts</name>
<desc/>
</dbinfo>
<variables></variables>
<chdbids></chdbids>
<record>
<record_id_>120</record_id_>
<update_id>1377875403275</update_id>
</record>
</qdbapi>

and that bit of code puts the value 120 into rid from the XML. I'm sending a search query (on a field that's not a key field) and getting the record ID back. The original code works fine.

Rob

  • New improved. Now with added something...
  • Jackass In Charge
  • Posts: 5959
  • Karma: +86/-149
  • Approaching 60 from the wrong damn direction...
Re: Gah! JQuery problem.
« Reply #10 on: October 16, 2013, 12:24:44 PM »
I'd add an alert after the get in the original then and see if it fires off.  Also, check the console as syntax problems will be reported there in chrome (and I assume FF)

That was the idea of the alert("success"), or should I put it somewhere different? No syntax errors in the console.

Mike

  • Jackass In Charge
  • Posts: 11248
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: Gah! JQuery problem.
« Reply #11 on: October 16, 2013, 12:29:34 PM »
The alert in the function tells you the call was successful.  An alert after the entire get() call will tell you if javascript barfed due to bad syntax.  If it fires you are good, if not then you might be right about the syntax.

Rob

  • New improved. Now with added something...
  • Jackass In Charge
  • Posts: 5959
  • Karma: +86/-149
  • Approaching 60 from the wrong damn direction...
Re: Gah! JQuery problem.
« Reply #12 on: October 16, 2013, 12:31:25 PM »
Duh. There was an error in the URL forming code (a '?' instead of a '&') that meant the get was silently failing. When I added Mikes code, I noticed that too, and changed it as well.