0

Here is my code,

function rdsxsVal() {
'use strict';
$.get("../cgi-bin/csps_rdsxs.cgi?r=" + Math.random(), function (data) {

    spsVal = JSON.parse(data);

I use the debugger in Opera and see, data = "<?xml version="1.0" encoding=...

the error is: Uncaught SyntaxError: Unexpected token <

so I have researched a lot and tried what I can with no success.

JSON.parse('"ssdata"'); does not fail. This is not useful other than it confirms the format I need to correct the error. I need single quotes outside the double quotes.

So I tried adding single quotes by, data = "'" + data + "'"; but this results in,

data = "'<?xml version="1.0" encoding=...

which is wrong, the single quotes are in the wrong place.

I thought maybe the " (double quotes) was a debugger effect so used,

data = "" + data;

but this had no effect.

I also tried replacing the " with ' but this did not work,

data = data.toString().replace("\"", '\'');

I have a feeling there is a simple answer but I'm just not seeing it.

Solution: So by using the suggestion from @chris-vdp I now have the following working code and from the debugger I can confirm it enters the .done section and not the .fail even though both sections are the same for some reason.

function rdspsVal() {
'use strict';
$.ajax({
    url: "../cgi-bin/csps_rdsps.cgi?r=" + Math.random(),
    dataType: "xml"
}).done(function (data) {
    spsVal = data;
    if (!rdspsInit) {
        home();
        rdspsInit = true;
    }
    rdspsVal();
    timer1();
}).fail(function (data) {
    spsVal = data;
    if (!rdspsInit) {
        home();
        rdspsInit = true;
    }
    rdspsVal();
    timer1();
});

}

in place of the original,

function rdspsVal(){
$.get("../cgi-bin/csps_rdsps.cgi?r="+Math.random(), function(data) {
 spsVal = JSON.parse(data);
}).done(function(){
    if(!rdspsInit){
     home();
     rdspsInit = true;
    }
     rdspsVal();
     timer1();
}).fail(function(){
    if(!rdspsInit){
     home();
     rdspsInit = true;
    }
    rdspsVal();
    timer1();
});

}

Kaamar
  • 3
  • 2
  • 3
    XML is not JSON, you can't parse it with the JSON object. – Matt Greer Jan 28 '14 at 16:35
  • You should learn first [what JSON is](http://stackoverflow.com/questions/383692/what-is-json-and-why-would-i-use-it). – Felix Kling Jan 28 '14 at 16:37
  • Thanks for the quick reply and it was a simple answer after all. In my defence I inherited a project including partially working js code and I have been working through bugs. This is the last one. As to why the code is written like this I still need to find out and then what it should be doing. – Kaamar Jan 28 '14 at 16:41
  • your fail block should be different than your done block. It is executed in the case where the server returns an error, ie a 500. – chris vdp Jan 29 '14 at 21:53

1 Answers1

0

$.get is shorthand for $.ajax. What you seem to be missing is the datatype. jQuery will parse your results for you.

$.ajax({
  url: "../cgi-bin/csps_rdsxs.cgi?r=" + Math.random(),
  success: success,
  dataType: "xml"
});

EDIT: example of assigning data to the variable using Promises.

$.ajax({
    url: "../cgi-bin/csps_rdsxs.cgi?r=" + Math.random(),
    dataType: "xml"
}).done(function(data) {
    spsVal=data;
});
chris vdp
  • 2,842
  • 2
  • 22
  • 35