0

Alright, I've got a very unique API for a service I run, in which the return values are displayed similarly to JSON (but not JSON), and it works very well with most languages, but now I face a problem accessing the returned values using Javascript only.

I have a PHP page which takes GET parameters, and then displays output like this: stapi=0,0,0,0,0,0,0,0&stlinks=Hu3L|http://www.example.com,3Ozq|https://www.example.com

That is the only output of the webpage, and what I'd like to do is collect these two variables into a Javascript Array to be displayed as a list. (The second variable I typically collect into matched pairs as you can see the separation with the "|")

Now I just need to know how to collect the "variables" from the output and put them into an array (these two pages are NOT going to be on the same server) ...

**I want to put the stapi comma-separated values into its own array and then the stlinks vertical-bar matched pairs/comma separated values into its own array

NickDodd
  • 13
  • 5
  • You might want to take a look at [this answer](http://stackoverflow.com/questions/901115/get-querystring-values-with-jquery/2880929#2880929) for parsing URL parameters. I can't quite tell if this is a duplicate question or not because I'm not sure how you expect the translation result to be - maybe you could update your question with a JSON representation of how the result should be structured in JS? – Andy E Mar 10 '11 at 15:08
  • I already saw that question, however I need to get the web page contents FIRST and then separate the 2 arrays and the second into matched pairs – NickDodd Mar 10 '11 at 15:30

1 Answers1

0

Try this function to split your string with this pipe:

function splitString(s,ch){
    var n = [];
    var i = 0, j = 0;
    while (i != -1){
        j = i;
        i = s.indexOf(ch,i+1);
        if (i > -1){
            if (j > 0){
                n.push( s.substr(j+ch.length,i-j-ch.length) );
            } else {
                n.push( s.substr(j,i-j) );
            }
        } else {
            n.push( s.substr(j+ch.length) );
        }
    }
    return n;
}

var result = [];

var pairs = splitString("stapi=0,0,0,0,0,0,0,0&stlinks=Hu3L|http://www.example.com,3Ozq|https://www.example.com","|");

var i = 0;

for (i = 0; pairs.length < i; i++){
    var temp = splitString(pairs[i],'=');
    result.push({name: temp[0], value: temp[1]});
}

What You need is packed to result variable.

Piotr Salaciak
  • 1,653
  • 1
  • 15
  • 28
  • How do I get the string from the php page though? thats the problem I'm facing, I need to retrieve the contents of the page first, then handle it's data – NickDodd Mar 10 '11 at 15:23
  • Are You able to use jQuery? jQuery.ajax({ url: "my_page.php", success: function(data){ } }); – Piotr Salaciak Mar 10 '11 at 15:37
  • Alright, that works...I was trying to use the shorthand version: $.get() - however now I think I'm facing that problem of same origin policy because as I mentioned, the script is a client script, not a server script, so It wont be on the same server....and therefor is not working on the remote script – NickDodd Mar 10 '11 at 15:50