【问题标题】:Jsonp not working with getJSON jqueryJsonp 不能与 getJSON jquery 一起使用
【发布时间】:2016-03-23 21:38:23
【问题描述】:
$(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 如果需要,该网址已开放供访问

【问题讨论】:

  • 你没有提供太多信息供人们在这里工作。你能包括一个返回的 JSON 的例子吗?也许只需对同一个 URL 执行 curl 请求?
  • 您的控制台中是否收到任何Access-Control-Allow-Origin 错误?
  • 当http://quanta.net16.net/wordpressnew/test.php?jsoncallback=?中的内容被执行时,它不会是一个完美的JavaScript。
  • 添加标题("Access-Control-Allow-Origin: *");在顶部的 php 文件中
  • 我收到此错误控制台 chrome XMLHttpRequest cannot load quanta.net16.net/wordpressnew/test.php。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'api.jquery.com' 不允许访问。响应的 HTTP 状态代码为 404。

标签: javascript php jquery json jsonp


【解决方案1】:

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>

更正了sn-p。检查控制台。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-22
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      相关资源
      最近更新 更多