jQuery 如何从 AJAX 调用返回 JSON 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9608975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 11:03:20  来源:igfitidea点击:

How to return JSON data from an AJAX call

jqueryajax

提问by sehummel

I have a JSON object I am returning from the database. It is formatted correctly. I am trying to access the data in it with an AJAX call. Here is my AJAX.

我有一个从数据库返回的 JSON 对象。它的格式正确。我正在尝试使用 AJAX 调用访问其中的数据。这是我的 AJAX。

$.ajax({
        url: '<?php echo site_url('find_representatives/find_rep_by_address/get_coordinates'); ?>', 
        dataType: 'json',
        data: '',
        success: function(data, status, xhr) {
             alert(data);
        },
        error: function(xhr, status, error) {
             alert(status);
        }
});

I have '' in my data because I am looking for all of the data. I tried putting 'id' there (there is an ID in my JSON object, but the function stopped working when I did that. When I alert 'data' I get an object, but where I alert 'data.id' I get 'undefined.' What am I doing wrong? This is my first AJAX call. The URL is valid. I checked.

我的数据中有 '' ,因为我正在寻找所有数据。我尝试将 'id' 放在那里(我的 JSON 对象中有一个 ID,但是当我这样做时该函数停止工作。当我警告 'data' 时,我得到一个对象,但是在我警告 'data.id' 的地方我得到了 '未定义。' 我做错了什么?这是我的第一次 AJAX 调用。URL 有效。我检查过。

采纳答案by Har

try data[0]and see what you get in an alert...been there i think this will help you

试着data[0]看看你在警报中得到了什么......去过那里我认为这会帮助你

回答by Mark Schultheiss

data: '{}', 

This sends an empty data object to the server and works around some issues where sending empty data (not including the dataat all) causes issues.

这会向服务器发送一个空数据对象,并解决一些发送空数据(根本不包括data)导致问题的问题。

One other thing I have seen is not setting:

我看到的另一件事是没有设置:

contentType: "application/json",

One easy way to "debug" data visually is to include json2.js and do (in the success function):

一种直观地“调试”数据的简单方法是包含 json2.js 并执行(在成功函数中):

alert(JSON.stringify(data));

回答by John Green

There isn't enough information to answer this question properly.

没有足够的信息来正确回答这个问题。

If you're trying to debug with 'Alert' though, you're in trouble.

但是,如果您尝试使用“警报”进行调试,那么您就有麻烦了。

Instead of 'alert(data)', try 'console.log(data)', assuming you're using FireBug or the Inspector (Chrome, Safari).

假设您使用的是 FireBug 或 Inspector(Chrome、Safari),而不是 'alert(data)',请尝试使用 'console.log(data)'。

Data may be several kinds of things, generally an object. So, alerting it won't do much for you, unless you turn it into a string first.

数据可能是几种事物,通常是一个对象。所以,除非你先把它变成一个字符串,否则提醒它对你来说没什么用。

You can also use the network panels to see what data is coming over the wire, or you can use something like Fiddler or HTTPScoop to figure out what is coming back from the server.

您还可以使用网络面板查看通过网络传输的数据,或者您可以使用 Fiddler 或 HTTPScoop 之类的工具来确定从服务器返回的数据。

回答by switz

Check out getJSON.

退房getJSON

$.getJSON(<?php echo site_url('find_representatives/find_rep_by_address/get_coordinates'); ?>, function(data) {
  console.log(data);
});

回答by bfavaretto

The datakey here is the one you want to send to the server (if any):

data这里的关键是你想发送到服务器的那个(如果有的话):

data: '',

But the dataargument inside the callbacks is what you receive back from the server:

但是data回调中的参数是您从服务器收到的:

success: function(data, status, xhr) {
    alert(data);
},
error: function(xhr, status, error) {
    alert(status);
}

When you use dataType: 'json', you are telling jQuery that you are expecting JSON back from the server. So, are you outputting valid JSON from PHP (e.g., with json_encode)? If so, your code should work.

当您使用 时dataType: 'json',您是在告诉 jQuery 您期望从服务器返回 JSON。那么,您是否正在从 PHP 输出有效的 JSON(例如,使用json_encode)?如果是这样,您的代码应该可以工作。