如果服务器对JSONP请求的响应未正确包装函数中的响应,是否有任何方法可以覆盖“无效标签”错误?

时间:2012-04-12 23:14:08

标签: javascript jquery jsonp

所以我正在尝试使用$ .JSON来抓取并访问此网址中的数据以用于辅助项目:https://api.pinterest.com/v2/users/cssquirrel/。从访问它可以看出,它是一个有效的JSON对象。

所以我使用jQuery执行以下JSONP请求。

$.getJSON('https://api.pinterest.com/v2/users/cssquirrel/?callback=?', function(data) { callback(data); });`

当我运行它时,我收到错误“invalid label”,因为虽然我收到了包含所有JSON信息的响应,但它没有将它包装在回调函数中,这导致一切都失败。

以下是回复:

{"status": "success", "user": {"username": "cssquirrel", "website": "", "about": "", "twitter_link": "", "publish_timeline": false, "is_following": false, "facebook_link": "http://www.facebook.com/cssquirrel", "image_url": "http://media-cache.pinterest.com/avatars/cssquirrel_1332472797.jpg", "location": "", "full_name": "Kyle Weems", "stats": {"followers_count": 10, "likes_count": 13, "pins_count": 259, "following_count": 24, "boards_count": 2}, "id": "123778827166945150", "image_large_url": "http://media-cache.pinterest.com/avatars/cssquirrel_1332472797_o.jpg"}, "boards": [{"category": "home", "is_collaborator": false, "user_id": "187251434415197561", "description": "This board is specifically for things we want to buy or am going to buy for redecorating our apartment in the next year or so (one can hope).", "url": "/jnaepalm/my-apartment-redecoration/", "is_following": false, "thumbnails": ["/uploads/cover_187251365695749104_187251296976783278_8e1oaKi9_1333652274.jpg", "http://media-cache.pinterest.com/upload/187251296976877180_C8VMZpRi_t.jpg", "http://media-cache.pinterest.com/upload/187251296976832689_ripauAtd_t.jpg", "http://media-cache.pinterest.com/upload/187251296976832240_fUHOQFUL_t.jpg", "http://media-cache.pinterest.com/upload/89509111314068749_b8HECGS4_t.jpg", "http://media-cache.pinterest.com/upload/187251296976813631_mqrILJ7L_t.jpg"], "stats": {"followers_count": 53, "pins_count": 11}, "id": "187251365695749104", "name": "My Apartment Redecoration"}, {"category": "architecture", "is_collaborator": false, "user_id": "123778827166945150", "description": "", "url": "/cssquirrel/dawww/", "is_following": false, "thumbnails": ["http://media-cache.pinterest.com/upload/125960120798214086_Q7SKFTww_222.jpg", "http://media-cache.pinterest.com/upload/211950726183236720_7Kod2qbJ_t.jpg", "http://media-cache.pinterest.com/upload/125045327123930771_LuvhIu65_t.jpg", "http://media-cache.pinterest.com/upload/87186942756265435_zkDGofnk_t.jpg", "http://media-cache.pinterest.com/upload/226165212507107947_FmCa7hLT_t.jpg"], "stats": {"followers_count": 17, "pins_count": 20}, "id": "123778758447498751", "name": "Dawww"}, {"category": "food_drink", "is_collaborator": false, "user_id": "123778827166945150", "description": "", "url": "/cssquirrel/nom-nom-nomophilia/", "is_following": false, "thumbnails": ["http://media-cache.pinterest.com/upload/236931630366384673_cIr5IfF1_222.jpg", "http://media-cache.pinterest.com/upload/280419514268240404_2ysv68TW_t.jpg", "http://media-cache.pinterest.com/upload/192388215301900735_YjfHY9zf_t.jpg", "http://media-cache.pinterest.com/upload/157414949445677782_ETiqDlZ4_t.jpg", "http://media-cache.pinterest.com/upload/284712007662990043_kKnxN3jz_t.jpg"], "stats": {"followers_count": 20, "pins_count": 80}, "id": "123778758447497411", "name": "Nom-nom-nomophilia"}, {"category": "men_apparel", "is_collaborator": false, "user_id": "123778827166945150", "description": "", "url": "/cssquirrel/project-sexypants/", "is_following": false, "thumbnails": ["http://media-cache.pinterest.com/upload/252201647852822759_uwlKOI1e_222.jpg", "http://media-cache.pinterest.com/upload/262264378270304794_88rVBmtm_t.jpg", "http://media-cache.pinterest.com/upload/198439927301238104_ZOX7SIcE_t.jpg", "http://media-cache.pinterest.com/upload/90775748709356579_2Zdk8if8_t.jpg", "http://media-cache.pinterest.com/upload/245657354644409113_GrKBq62Q_t.jpg"], "stats": {"followers_count": 20, "pins_count": 33}, "id": "123778758447497410", "name": "Project Sexypants"}, {"category": "cars_motorcycles", "is_collaborator": false, "user_id": "123778827166945150", "description": "", "url": "/cssquirrel/cars-to-overcompensate-with/", "is_following": false, "thumbnails": ["http://media-cache.pinterest.com/upload/216946907019857635_qiGKPgTa_222.jpg", "http://media-cache.pinterest.com/upload/30258628716631205_PAfHj9Ds_t.jpg", "http://media-cache.pinterest.com/upload/285486063848979846_cZABTXYA_t.jpg", "http://media-cache.pinterest.com/upload/193936327673799615_3wdGZJ7U_t.jpg", "http://media-cache.pinterest.com/upload/253749760224841585_f5N7CY4h_t.jpg"], "stats": {"followers_count": 20, "pins_count": 44}, "id": "123778758447497409", "name": "Cars To Overcompensate With"}, {"category": "geek", "is_collaborator": false, "user_id": "123778827166945150", "description": "", "url": "/cssquirrel/geeeeeek/", "is_following": false, "thumbnails": ["http://media-cache.pinterest.com/upload/123778689728702748_OuOah7Ki_222.jpg", "http://media-cache.pinterest.com/upload/123778689728702742_2MXhVgEb_t.jpg", "http://media-cache.pinterest.com/upload/152418768608872302_FTHuHi9M_t.jpg", "http://media-cache.pinterest.com/upload/160792649165450534_EAB8aKAI_t.jpg", "http://media-cache.pinterest.com/upload/169307267210994707_D0Ht2JrH_t.jpg"], "stats": {"followers_count": 20, "pins_count": 48}, "id": "123778758447497408", "name": "Geeeeeek"}, {"category": "food_drink", "is_collaborator": false, "user_id": "123778827166945150", "description": "", "url": "/cssquirrel/brew/", "is_following": false, "thumbnails": ["http://media-cache.pinterest.com/upload/135389532518003791_9awFAkAM_222.jpg", "http://media-cache.pinterest.com/upload/87116574011433685_49ZFZZUy_t.jpg", "http://media-cache.pinterest.com/upload/44050902575271742_HOZYAXOW_t.jpg", "http://media-cache.pinterest.com/upload/219620919298640329_yQlav4EP_t.jpg", "http://media-cache.pinterest.com/upload/123778689728668385_W9OfOjAj_t.jpg"], "stats": {"followers_count": 20, "pins_count": 34}, "id": "123778758447497407", "name": "Brew"}]}

有没有办法让我处理这个JSON对象,尽管“无效标签”错误? (不更改我无权访问的服务器端代码)。显然,我想要的数据正在被发送,它只是缺少JSONP通常期望的函数包装器。

1 个答案:

答案 0 :(得分:1)

如果服务器仅响应JSON并且不期望JSONP请求,并且您无法访问服务器,那么就无法使其工作。 (如果你可以使它工作,那么任何返回JSON作为其正常操作一部分的站点都会是一个很大的安全漏洞,因为这会允许跨站点数据窃取。)

因此,除非服务器想通过启用CORS或通过执行JSONP来执行此操作,否则无法以这种方式获取数据。