开发者

Using JSON to get Javascript code from another domain

I'm having some trouble getting JSON to store some Javascript as a string value.

{
"code": "function test(){alert('coreg.js has been loaded'); if (jQuery) { alert('We have jQuery!'); } else { alert('We don\'t have jQuery...'); } }"
}

When I validate this using www.jsonlint.com it says I've got a syntax error (unexpected TINVALID at line 2) but I can't for the life of me see what it is. The Javascript is all entirely valid, all I did was take my function and put double quotes outside it.

I'm really conf开发者_如何学运维used! Does anyone know what I'm doing wrong?


you need to escape the escape: We don\\'t have jQuery

{
"code": "function test(){alert('coreg.js has been loaded'); if (jQuery) { alert('We have jQuery!'); } else { alert('We don\\'t have jQuery...'); } }"
}

though if you are using json to get JS code from another domain, you wont be able to do it unless the other domain supports JSONP, or you are using grease monkey's XHR

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜