简体   繁体   中英

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 confused! 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM