0

I am trying to create a function which will take a users input and use the spotify API to search for matching tracks.

This code works with the URL pointing to an internal JSON file for testing, but does not even make a request as far as I can tell when the spotify URL is entered: not even the request.always case sends out an alert. Instead, it shows the last alert ("here"). I have looked at network requests and there are none sent when the function is called. Making a request with curl in terminal works, so I know that the url is valid and working (with the search parameter filled in manually), so I am now at a loss why it won't work in this particular Javascript function.

I've been trying to fix this for over 3hrs now and any input is much appreciated. Cheers :)

function SearchForTrack(searchParameter) {
    var request = $.get("https://api.spotify.com/v1/search?q=" + searchParameter + "&type=track");
    request.done(function(data) {
      alert(data);
      DisplaySearchResults(data);
    });
    request.fail(function(jqXHR, textStatus, errorThrown) {
      if (textStatus == 'timeout')
        console.log('The server is not responding');
      if (textStatus == 'error')
        console.log(errorThrown);                       
    });
    request.always( function(){
      alert("hello");
    });
    alert("here");
}
5
  • 1
    there´s nothing wrong with your code... just tried it and it worked Commented Jul 28, 2015 at 16:43
  • yes. it worked in my case too. Commented Jul 28, 2015 at 16:47
  • Ok thats good to know, I will try It on my server and see if I get different results. Cheers Commented Jul 28, 2015 at 16:53
  • Why DOES this work though? I always thought that cross-domain access using Ajax was not possible on most browsers (unless you employed JSONP). Yet this JSfiddle definitely works! Can someone explain? Commented Jul 28, 2015 at 17:32
  • @cars10 - because CORS. Commented Jul 28, 2015 at 18:02

1 Answer 1

1

There's nothing wrong with your code. Take a look at this JSFiddle that is running almost the same code: http://jsfiddle.net/66rwob2w/

Note that you will receive the alert("here") message first because the others alert()s are inside callbacks, so they will be requeued on the JS event loop. After that you will receive both the alert(data) and alert("hello"). If this was the behaviour that made you think your code was wrong, take another look!

In my example I removed your function and used an IIFE (Immediately-Invoked Function Expression) to call your code. I also removed the string between the URL and set a fixed string sugar. You might try some of these tweaks on your code to see if they work.

Also I would recomendo switching from alert() to console.log() and test it with your browser Developer Tools opened. That´s less intrusive and less boring to test!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.