0

I'm having some issues trying to send and receive parameters in javascript and Ajax. I´m sending to /api/getHighScores and that function requires a timestamp for input and the output is a json-file with user information and a value, the highscore. When I receive the response, I want it to print in my Chrome plugin as well.

This is the code right now.

data = {
    "startTime": JSON.stringify(1490208166633),
}
function getHighscores() {
    $("#knapp2").click(function() {

         $.ajax({
             type:"GET",
             url: "/api/getHighscores",
             data:[startTime=1490208166633],
             success: function (response){}
         });
    });
}

{
    "manifest_version": 2,

    "version": "1.1",

    "browser_action": {
      "default_icon": "resources/img/clock-icon-png-10763.png",
      "default_popup": "popup/popup.html"
    },
    "background": {
      "scripts": ["background/background.js"],
      "persistent": false
    },
    "permissions": [
      "activeTab",
      "https://ajax.googleapis.com/"
    ]
}
CDspace
  • 2,639
  • 18
  • 30
  • 36
Cia
  • 13
  • 5
  • 1
    What are the issues, you are facing? – Lakmal Vithanage Apr 05 '17 at 08:31
  • I don´t think I do POST an GET the correct way, and when using console log it doesn´t print the response. – Cia Apr 05 '17 at 08:33
  • @Cia, did you actually open the [background script console](https://stackoverflow.com/a/10258029)? – wOxxOm Apr 05 '17 at 08:52
  • Nice one, yes I did :D When I push the button to show the highscores, nothing happens – Cia Apr 05 '17 at 09:00
  • The syntax of data is unexpected in ajax: why not curly braces and colon? Also, there's nothing in your code that prints to console. – wOxxOm Apr 05 '17 at 09:28
  • Look in the network panel in background devtools to see what's actually sent. – wOxxOm Apr 05 '17 at 09:30
  • Is that ajax call in the popup script or the background script? Those have different consoles. Right click the popup and Inspect it to see its devtools. – wOxxOm Apr 05 '17 at 09:32

1 Answers1

0

I've made a few assumptions here about your API structure but this is what I would expect to be.

Typically the

api/getHighScores

endpoint would get all the high scores independent of the time stamp variable.

var data = { startTime: 1490208166633 };
var userInfomation;

$("#knapp2").click(function() {
    $.ajax({
        type:"GET",
        url: "/api/getHighscores/" + data.startTime,
        success: function (userInfo)
        {
            userInfomation = userInfo;
        }
        error: function (response) 
        {
            // you should deal with the error case as well
            // it could give you more information about whats going wrong.
        }
    });
});
function getHighscores() {
    $("#knapp2").trigger("click");
}

If you are actually sending data under a GET method.

var data = { startTime: 1490208166633 };
var userInfomation;

$("#knapp2").click(function() {
    $.ajax({
        type:"GET",
        url: "/api/getHighscores",
        data: JSON.stringify(data);
        success: function (userInfo)
        {
            userInfomation = userInfo;
        }
    });
});
Andrew
  • 81
  • 3