简体   繁体   English

如何获取jQuery .get返回JSON数据?

[英]How can I get jQuery .get to return JSON data?

I am trying to fetch information using AJAX from a URL. 我正在尝试使用AJAX从URL获取信息。 This URL will return a JSON response but I am having a great deal of trouble getting this to work. 该URL将返回JSON响应,但是要使其正常工作,我遇到了很多麻烦。 I am fairly new to using both AJAX and JSON so I am not quite sure as to what I am doing wrong. 我对同时使用AJAX和JSON还是很陌生,所以我不确定自己做错了什么。 I am not receiving any output. 我没有收到任何输出。 Here's what I have so far: 这是我到目前为止的内容:

HTML: HTML:

<!doctype html>
<html>
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content - Type">
    <meta content ="utf-8" http-equiv="encoding">

    <title>My Javascript Practice</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <noscript>JavaScript Must Be Enabled</noscript>
</head>
<body>
    <div id="pub">Parent Div</div>

    <script type="text/javascript" src="getList.js"></script>
</body>
</html>

JavaScript: JavaScript的:

var teamId = 883455;
var myUrl = "https://apps-internal.attributor.com/guardianapi/service/csadminservice/csteams/" + teamId + "?view=PUBLISHERS_FOR_TEAM";

$.get(myUrl, function(data){
    $("#pub").html(data);
    alert("load was performed");
});

May I suggest to use something like this: 我可以建议使用如下形式:

$.ajax({
  type: 'GET',
  url: myURL,
  data: yourDAta,
  dataType: 'jsonp',
  success: function(jsonData) {
    alert(jsonData);
  },
  error: function() {
    alert('Error loading ');
  }
});

Note the usage of jsonp over json 注意jsonpjson的用法

Try using the getJSON() method instead if you're only interesting in getting back a JSON response. 如果仅对获取JSON响应感兴趣,请尝试使用getJSON()方法。

The .get, .load, .getJSON are all just extensions that use the .ajax method underneath, if you can't get the extension method working it sometimes helps to just use straight .ajax() .get,.load和.getJSON都是在下面使用.ajax方法的扩展名,如果您无法使用该扩展方法,有时仅使用直接.ajax()会有所帮助

Essentially the getJSON() method is just this: 本质上,getJSON()方法就是这样:

$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: success
});

Note the explicit use of the dataType: "json". 请注意dataType:“ json”的显式使用。

Since this looks like a cross-domain call you will need to use something like jsonp (JSON with padding) or CORS (Cross-origin resource sharing) if your endpoint supports it. 由于这看起来像是跨域调用,因此如果端点支持,则需要使用jsonp(带填充的JSON)CORS(跨域资源共享)之类的东西。 If jsonp is supported by your endpoint you can set the dataType: "jsonp" but it needs to be explicitly supported by the server see this post for more details on jsonp. 如果JSONP是由您的端点支持,您可以设置数据类型:“JSONP”,但它需要由服务器来明确支持看这个职位上JSONP更多细节。

Note: your server API must support jsonp or CORS for this to work. 注意:您的服务器API 必须支持jsonp或CORS才能起作用。

just add json as the third parameter, the data passed to the callback will be an object representation of the received json string 只需添加json作为第三个参数,传递给回调的数据将是接收到的json字符串的对象表示形式

this should work, 这应该工作,

var teamId = 883455;
var myUrl = "https://apps-internal.attributor.com/guardianapi/service/csadminservice/csteams/" + teamId + "?view=PUBLISHERS_FOR_TEAM";

$.get(myUrl, function(data){
    //data here will be object, should not used directly
    $("#pub").html(data);
    alert("load was performed"); 
}, 'json');

if you are on different domain, you could setup a server side script to grab that data, say it is php file called api.php 如果您在不同的域中,则可以设置服务器端脚本以获取该数据,说这是名为api.php的php文件。

<?php
    $teamId = $_GET['teamId'];

    //php.ini should allow url fopen
    $response = file_get_contents("https://apps-internal.attributor.com/guardianapi/service/csadminservice/csteams/". $teamId ."?view=PUBLISHERS_FOR_TEAM");
    echo $response;
?>

and call it in your js file 并在您的js文件中调用

var teamId = 883455;
var myUrl = "path/to/api.php?teamId="+teamId;

$.get(myUrl, function(data){
    //data here will be object, should not used directly
   console.log(data);
}, 'json');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM