/    Sign up×
Community /Pin to ProfileBookmark

Convert ajax response to json

I am using ajax to process a url. The result echoed back is

ok=true

how can I convert that to json format so I can read that back in?

I can’t edit how the response comes to me.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@rnd_meFeb 16.2011 — I am using ajax to process a url. The result echoed back is

ok=true

how can I convert that to json format so I can read that back in?

I can't edit how the response comes to me.[/QUOTE]


[CODE]function parseQS(str){
var ob={}, float="", key="", dc=decodeURIComponent;

for( var i=0, mx=str.length; i<mx;i++){
var it=str[i];
if(it==="="){ key=float; float=""; continue;}
if(!it.search(/^[?&]/)){
if(it==="&" && str.slice(i+1,i+5)==="amp;"){ i=(i+4);float+="&"; continue;}
if(key){ob[key]=dc(float);} key=""; float=""; continue;
}
float+=it;
}
ob[key]=dc(float);
return ob;
}



var ob=parseQS("ok=true");
var str=JSON.stringify(ob);
alert(str); //shows: {"ok":"true"} [/CODE]
×

Success!

Help @swg1cor14 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...