0

I am quite new to jQuery, and I am having a difficult time querying an api with jQuery.

There appears to be no syntax error but code is not firing - all the variables and functions have been defined

$.get(githubsearch).success(function(r){
    displayResults(r.items);
})             
.fail(function(err){
    console.log("failed to query Github");
})
.done(function(){
});
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
  • There's no `.success` function in a [Deferred object](https://api.jquery.com/category/deferred-object/) (which is what [$.get](https://api.jquery.com/jquery.get/) returns). Move `displayResults(r.items);` into the `done` section and you might have more luck. – ADyson May 29 '20 at 13:53
  • (P.S. `success` is an option you can send to $.get to set a callback there (as an alternative to using the Deferred interface), so maybe you got it confused with that. Try and study examples closely and make sure you get the specific details right.) – ADyson May 29 '20 at 13:55

2 Answers2

2

There is no success function in $.get

You can also use $.ajax. Find difference here

I am writing both ways whatever you like.

$.ajax({
        url: githubsearch,
        type: 'get',
        success: function (data) {
             displayResults(data);
        },
        error: function(err){
          console.error(err);
        }
}).done(function() {
      console.log("finished");
});

2nd Example

$.get( githubsearch, function(data) {
   displayResults(data);
})
.done(function() {
    console.log("finished");
})
.fail(function() {
     console.error(err);
})
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
Muhammad Sami
  • 520
  • 2
  • 8
  • 21
-2

The anser is simple, your code is not correct use this.

var data = {
    name: "Test"
}
var url = "https://www.api.site.com/v1/getuser" //example site
//you can remove data if not required
//request can be get or post
$.post(url, data, function (response) { 
//    success data
    console.log(response)
}).fail(function (response) {
//    failed data
    console.log(response)
});

Please format your code before posting.

Deviser
  • 83
  • 1
  • 11
  • Why $.post instead of $.get? This looks like a general example unrelated to the actual question. Also, "use this" isn't really a very good explanation. _Why_ should someone use this? _Why_ would it solve the problem? Not explaining your code encourages blind copy/pasting of things which may not suit every situation. – ADyson May 29 '20 at 14:27
  • Why $.post instead of $.get?-> i already write the comment about this. Use this -> because this will solve the problem. Why should someone use this?-> again because this will solve the problem. Why would it solve the problem? -> read compiler. And this is not copy/paste. Downvoting the answer not mean answer is wrong. you need to check what comments are for. – Deviser May 29 '20 at 14:53
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. See https://meta.stackoverflow.com/questions/300837/what-comment-should-i-add-to-code-only-answers Not that: *"your code is not correct, this is correct"* does not provide "additional context". – freedomn-m May 29 '20 at 14:59
  • 1
    Couple of notes that warrant down-voting (FYI, if you care...) - "answer is simple" is too condescending - if it was simple, they wouldn't be asking it. "your code is not correct" - obviously, otherwise they wouldn't be asking. Explanation in code comments is ok (as referenced in comment above) but there's no explanation (assuming "read compiler" means "read code"). OP's code uses `$.get` then suggesting (even with comment for alternative) to use `$.post` will likely cause a lot of confusion if the server isn't configured for either. Answers should be tailored to the question. Good Luck. – freedomn-m May 29 '20 at 15:05
  • _"Use this -> because this will solve the problem"_ is not a reason, that's just a tautology. it doesn't explain _why_ it solves the problem. That was my point. And _"Why would it solve the problem? -> read compiler"_ ...I have no idea what this means. What "compiler" are you talking about? This sentence doesn't clarify anything at all. _"Downvoting the answer not mean answer is wrong"_ ...this part is true at least. Downvoting answers is given because they are "not useful" (I'm quoting from the text when you hover over the button). An answer can be technically correct but still not useful. – ADyson May 29 '20 at 16:03
  • _"this is not copy/paste"_ .... I never said it was. I said it would _encourage_ blind copy/pasting of your answer, due to the lack of explanation. I think you haven't understood my comment fully. – ADyson May 29 '20 at 16:06