我正在使用JIVE JSON RESTFul API,并且在他们的API上有一个安全性
(我指的是throw' allowIllegalResourceCall是假的。&#39 ;;)
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myObj = JSON.parse(this.responseText);
document.getElementById("topdisplay").innerHTML = myObj.id;
}
};
xmlhttp.open("GET", "http://mysite/API/",true);
xmlhttp.send();
我正在使用此代码尝试PARSE IT:
static void Main(string[] args)
{
int heads = 0;
int tails = 0;
int counter = 0;
Random coinflip = new Random();
Console.WriteLine("How many times would you like to flip a coin? ");
counter = Convert.ToInt32 (Console.ReadLine());
for (int i = 0; i < counter; i++)
{
int flip = coinflip.Next(1, 3);
if (flip == 1)
{
heads++;
}
else
{
tails++;
}
}
Console.WriteLine("You flipped a coin " + counter
+ "times " + "and you got " + heads + "heads and " + tails + "tails.");
Console.WriteLine();
}
我因为起跑线而收到错误。 我到处寻找尝试找到一个解决方案,跳过JSON PARSE可以工作,但我似乎无法找到一种方法来做到这一点。
我也知道解析代码有效,因为当我删除第一行时,它完美地工作。
有任何帮助吗?
答案 0 :(得分:-2)
JIVE REST API引入了这一点,以帮助防止在Web浏览器易受攻击时反击JSON Hijacking。
您需要先找到{
,然后从该位置到字符串末尾执行子字符串。
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var response = this.responseText
var myObj = JSON.parse(this.responseText.substring(this.response.indexOf('{')));
document.getElementById("topdisplay").innerHTML = myObj.id;
}
};
xmlhttp.open("GET", "http://mysite/API/",true);
xmlhttp.send();
注意:强>
使用RegEx,你需要找到第一行带有throw并以分号结尾,如果找到则替换为空字符串。
JSON.parse(response.replace(/throw.*;/, "").trim());