Jsonp 不能与 getJSON jquery 一起使用

Jsonp not working with getJSON jquery

本文关键字:一起 jquery getJSON 不能 Jsonp      更新时间:2023-09-26
$(document).ready(function() {
  $.getJSON("http://quanta.net16.net/wordpressnew/test.php?jsoncallback=?", function(data) {
    alert('swag');
  });
});

这是我的 JSON 调用,我的 JSON 正确包装在 ?({对象}(但是警报没有弹出,它没有显示任何JavaScript错误请帮助解决任何可能的修复问题链接到创建 PHP 的 JSON如果需要,URL 是开放的

,可供访问

请求的输出为:

?([{"id":84,"title":"Solar Decathlon","url":"http:'/'/quanta.net16.net'/wordpressnew'/wp-content'/uploads'/2015'/12'/grp4_s2.jpg","isFeatured":false,"isNews":false,"college":false,"time":1450076653,"summary":""},{"id":80,"title":"HUDCO UNIT 2012-13","url":false,"isFeatured":false,"isNews":false,"college":false,"time":1450076349,"summary":"HUDCO UNIT 2012-13 got the first citation in 55th annual NASA convention 2012-13 for their design of 24*7 day and night permanent shelter for urban homeless.The entire work is taken by HUDCO,New delhi to work more on it.Also the entire work has been asked to sent by Dr.amod kumar from st.stefens hospital,tiss hazati, Delhi. and […]"},{"id":77,"title":"Egyptian Artwork","url":"http:'/'/quanta.net16.net'/wordpressnew'/wp-content'/uploads'/2015'/12'/XU0zTIXjslCgCGAULTlAVz3AEbLzNFdsB1OVFJH7zWYw222-h220.jpg","isFeatured":false,"isNews":false,"college":false,"time":1450075669,"summary":"Made By the students of all the batch 3102 etc,this is some artwork info.Made By the students of all the batch 3102 etc,this is some artwork infoMade By the students of all the batch 3102 etc,this is some artwork infoMade By the students of all the batch 3102 etc,this is some artwork infoMade By the […]"},{"id":67,"title":"Collezione – 2015","url":"http:'/'/quanta.net16.net'/wordpressnew'/wp-content'/uploads'/2015'/12'/event2015.jpg","isFeatured":false,"isNews":false,"college":false,"time":1450074883,"summary":"Collezione – 2015 the Annual Fashion Show of the School Of Fashion & Textile Design, The new age designers put a jaw dropping collections, for various category of Indian Ethnic, Resort, Cruize, Swimwear, Westernwear formal & men'u2019s wear."},{"id":63,"title":"International Tour: 'u201cWorld is a book, those who do no travel read only one page'u201d 'u2013 St. Agustine","url":"http:'/'/quanta.net16.net'/wordpressnew'/wp-content'/uploads'/2015'/12'/irfw_23.jpg","isFeatured":false,"isNews":false,"college":false,"time":1450071633,"summary":"True to ever word, students along with our experienced faculty get to have a great exposure to world of Fashion Art & Design. We host premium events with visits to Premier Vision, Art galleries, Meeting Designers and much more for an unmatched international exposure and witness latest in fashion, upcoming trend & So forth. Come […]"},{"id":53,"title":"yoloswag asdfasdfa","url":false,"isFeatured":false,"isNews":false,"college":false,"time":1449835439,"summary":"asdfasdf"},{"id":50,"title":"asdf","url":false,"isFeatured":false,"isNews":false,"college":false,"time":1449834299,"summary":"asdfasdfasdasdfasdfasdfsdfasd"},{"id":48,"title":"asdfasdf","url":false,"isFeatured":false,"isNews":false,"college":false,"time":1449834004,"summary":"asdfasdfasdfasdf"},{"id":45,"title":"asdfasdfasdfasdfad","url":false,"isFeatured":false,"isNews":false,"college":false,"time":1449833570,"summary":"adsfasdfasdfasdfasdfasdfasdf"},{"id":43,"title":"Story of my life","url":false,"isFeatured":false,"isNews":false,"college":false,"time":1449830857,"summary":""}]);

这不是一个有效的JavaScript。函数调用不能以 ?() 开头。相反,我会要求你创建一个使用它的函数,比如:

function callJson (data) {
  alert(data.length);
  console.log(data.length);
}

现在,只需向以下人员提出请求:

http://quanta.net16.net/wordpressnew/test.php?jsoncallback=callJson

这给出了输出,callJson({json-data}).

工作片段

$(function () {
  function callJson (data) {
    alert(data.length);
    console.log(data.length);
  }
  $.getJSON("https://jsonp.afeld.me/?url=http://quanta.net16.net/wordpressnew/test.php?jsoncallback=callJson", function (data) {
    debugger;
  }).fail(function(data) {
    // console.log( data );
    res = data.responseText.trim().replace("callJson(", "");
    res = res.substr(0, res.length-2);
    res = JSON.parse(res);
    console.log(res);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

更正了代码段。检查控制台。

我没有正确设置跨源资源共享标头