如何从AJAX调用返回JSON数据

时间:2022-10-18 14:06:41

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.id'我得到'未定义。'我做错了什么?这是我的第一个AJAX调用。该URL有效。我查了一下。

5 个解决方案

#1


2  

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

尝试数据[0],看看你在警报中得到了什么...在那里我认为这会对你有所帮助

#2


6  

data: '{}', 

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

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

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和do(在success函数中):

alert(JSON.stringify(data));

#3


1  

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),请尝试'console.log(data)',而不是'alert(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之类的内容来确定从服务器返回的内容。

#4


0  

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

此处的数据键是您要发送到服务器的数据键(如果有):

data: '',

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

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

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)?如果是这样,您的代码应该可行。

#5


0  

Check out getJSON.

看看getJSON。

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

#1


2  

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

尝试数据[0],看看你在警报中得到了什么...在那里我认为这会对你有所帮助

#2


6  

data: '{}', 

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

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

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和do(在success函数中):

alert(JSON.stringify(data));

#3


1  

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),请尝试'console.log(data)',而不是'alert(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之类的内容来确定从服务器返回的内容。

#4


0  

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

此处的数据键是您要发送到服务器的数据键(如果有):

data: '',

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

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

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)?如果是这样,您的代码应该可行。

#5


0  

Check out getJSON.

看看getJSON。

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