jsÔõôµ¼ÈëjsonÎļþ
ÔÚ javascript Öе¼Èë json Îļþ£¬°ì·¨ÈçÏ£º½¨Éè xmlhttprequest ¹¤¾ß£ºconst request = new xmlhttprequest();ÉèÖÃÇëÇóÀàÐÍºÍ url£ºrequest.open(‘get’, ‘data.json’);ÉèÖÃÇëÇóÍ·£ºrequest.setrequestheader(‘content-type’, ‘application/json’);·¢ËÍÇëÇó£ºrequest.send();ÕìÌýÏìÓ¦£ºrequest.onload = function
ÔõÑùÔÚ JavaScript Öе¼Èë JSON Îļþ
µ¼Èë JSON ÎļþµÄ°ì·¨£º
½¨Éè XMLHttpRequest ¹¤¾ß£º
const request = new XMLHttpRequest();
µÇ¼ºó¸´ÖÆ
ÉèÖÃÇëÇóÀàÐÍºÍ URL£º
request.open('GET', 'data.json');
µÇ¼ºó¸´ÖÆ
ÉèÖÃÇëÇóÍ·£º
request.setRequestHeader('Content-Type', 'application/json');
µÇ¼ºó¸´ÖÆ
·¢ËÍÇëÇó£º
request.send();
µÇ¼ºó¸´ÖÆ
ÕìÌýÏìÓ¦£º
request.onload = function() { if (request.status === 200) { // ÆÊÎö JSON Îļþ const data = JSON.parse(request.responseText); // ʹÓÃÊý¾Ý... } };
µÇ¼ºó¸´ÖÆ
ʾÀý£º
// ½¨Éè XMLHttpRequest ¹¤¾ß const request = new XMLHttpRequest(); // ÉèÖÃÇëÇóÀàÐÍºÍ URL request.open('GET', 'data.json'); // ÉèÖÃÇëÇóÍ· request.setRequestHeader('Content-Type', 'application/json'); // ·¢ËÍÇëÇó request.send(); // ÕìÌýÏìÓ¦ request.onload = function() { if (request.status === 200) { // ÆÊÎö JSON Îļþ const data = JSON.parse(request.responseText); // Êä³öÊý¾Ý console.log(data); } };
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇjsÔõôµ¼ÈëjsonÎļþµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡
ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£