0

I'm making a cross domain call to a php which returns this:

response=2&count=15&id=84546379&firstname=adsa&emailstatus=

I want to pick out the values of response and id, but not sure how, my code is as follows:

**

xhr.request({
                    url: "../promo_getstate2.php",
                    method: "POST",
                    data: {
                      email: emailaddress,
                      country: country,
                      lang: lang,
                      source: '1312_XMAS_dly'
                    }
                }, function(response){
                   getstate = response['response'];
                   regID = response['id'];
                   console.log(getstate)
                    console.log(regID)
})

but it's not geting those values. How do I do this?

The response is:

" response=2&count=15&id=84546379&firstname=adsa&emailstatus="

**

user1937021
  • 8,155
  • 20
  • 68
  • 129
  • paste the output for your response, we don't know the format and value of the response. – Hasanavi Oct 31 '14 at 10:42
  • check out this http://aameer.github.io/articles/cross-origin-resource-sharing-cors/. Hope it helps – Aameer Oct 31 '14 at 10:43
  • Possible Duplicate of : http://stackoverflow.com/questions/789755/how-can-i-convert-query-string-or-json-object-map-to-single-json-object-with-jqu – Manish Kr. Shukla Oct 31 '14 at 10:43
  • Your response is like this `response=2&count=15&id=84546379&firstname=adsa&emailstatus=`? – Jenson M John Oct 31 '14 at 10:44
  • yes well it's response=2&count=15&id=84546379&firstname=adsa&emailstatus= – user1937021 Oct 31 '14 at 10:45
  • "it's not geting those values" — what is it getting? Look at your browser's developer tools. Look at the JavaScript console. Does it report any errors? Look at the Net tab. Is the request being made? Does it get a response? Do they contain the data you expect? – Quentin Oct 31 '14 at 10:49
  • this question has an answer: http://stackoverflow.com/a/2091331/280539 – JimiDini Oct 31 '14 at 10:51
  • @user1937021 you can use dataType:"json" in your ajax request, use it in **success** method like response.response, response.id – chanchal Oct 31 '14 at 10:52

2 Answers2

2

What you can do is create a params object of all parameters in the response as shown below:

function parseResponse(str) {
    var arr = str.split("&");
    var temp, params = [];
    for(var i = 0; i < arr.length; ++i) {
        temp = arr[i].split("=");
        params[temp[0]] = temp[1];
    }
    return params;
}

var values = parseResponse("response=2&count=15&id=84546379&firstname=adsa&emailstatus=")

You can then access values as:

values['response']; // 2

values['id']; // 84546379

Vikram Deshmukh
  • 3,067
  • 2
  • 26
  • 34
0

Code as that:

<Script language="javascript">
        var vars=GetRequest("response=2&count=15&id=84546379&firstname=adsa&emailstatus=");
        document.write(JSON.stringify(vars));

        function GetRequest(v) {
           var url = "?"+v;//location.search;
           var theRequest = new Object();
           if (url.indexOf("?") != -1) {
              var str = url.substr(1);
              strs = str.split("&");
              for(var i = 0; i < strs.length; i ++) {
                 theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
              }
           }
           return theRequest;
        }
        </script>

Then you can get the values. For example, the json of the result is

{"response":"2","count":"15","id":"84546379","firstname":"adsa","emailstatus":""}
Sinri Edogawa
  • 301
  • 1
  • 6