Jsonp不使用getJSON jquery

时间:2015-12-17 12:09:16

标签: javascript php jquery json jsonp

$(document).ready(function() {
  $.getJSON("http://quanta.net16.net/wordpressnew/test.php?jsoncallback=?", function(data) {
    alert('swag');
  });
});

这是我的json调用,我的json被正确包装?({object}) 但警报没有弹出它不会显示任何javascript错误 请帮助解决任何可能的问题 link to json creating php 如果需要,该URL将打开以供访问

2 个答案:

答案 0 :(得分:0)

request的输出是:

?([{"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>
&#13;
&#13;
&#13;

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

答案 1 :(得分:0)

我没有正确设置Cross Origin Resorce Sharing标头