通过jQuery AJAX请求获得JSON数据

时间:2022-12-04 16:15:23

I've having issues with parsing JSON data from a php file through to JQuery.I've had quite a difficult time finding the most appropriate way of doing this and any help would be appreciated. It needs to be in a function

从php文件到JQuery,我一直在解析JSON数据。我很难找到最合适的方法来做这件事,我很感激你的帮助。它需要在函数中

At the moment I've gotten to the point of:

此刻,我已经到了:

$.ajax({

    url: "Scripts/Interactions.php",
    type: "POST",
    dataType: "json",
    success: function(data){
        $.each(data, function(i, grab){
        alert(grab.AgentFullName);

        })

    }

})

While this works, one issue is it presents a row 'Undefined' before the first row, no matter what column I suggest.

尽管这样做是可行的,但有一个问题是,不管我建议的是哪一列,它在第一行之前都显示一个“未定义”的行。

Here is a sample of my Json Output data. It's mostly just random Ipsum Lorem at this text, as I'm still in early stages of development. I've checked the data through various online json format checkers and it's come back as valid.

下面是我的Json输出数据的示例。在这篇文章中,它基本上只是随机的Ipsum Lorem,因为我还处于开发的早期阶段。我已经通过各种在线json格式检查器对数据进行了检查,结果是有效的。

http://pastebin.com/KFKtiSD4

http://pastebin.com/KFKtiSD4

Thanks in advance for any help!

谢谢你的帮助!

2 个解决方案

#1


0  

[
   "results",
   {
      "InteractionID":"1",
      "AgentFullName":"Peter Germein",

referring the beginning of your json, you probably want to put "results":, instead of "results",

引用json的开头,您可能想要输入“results”:,而不是“results”,

#2


2  

The first element in your array is not an object, it's a string. That's why you're getting undefined for the first element when you say grab.AgentFullName—the string "results" has no such property.

数组中的第一个元素不是对象,而是一个字符串。这就是为什么当你说抓取时,第一个元素没有定义。agentfullname—字符串“结果”没有这样的属性。

You could change

你可以改变

[
   "results",
   {
      "InteractionID":"1",
      "AgentFullName":"Peter Germein",
      "InteractTopics":"Behaviour, Attendance, Attitude, Performance, Closing",
      "InteractDiscussion":"Cras at nisl lorem, a lacin...",
      "InteractAction":"Morbi quis nunc in odio eg...",
      "InteractNotes":"Quisque et ante ut nis..."
   },

to

[
   {
      "InteractionID":"1",
      "AgentFullName":"Peter Germein",
      "InteractTopics":"Behaviour, Attendance, Attitude, Performance, Closing",
      "InteractDiscussion":"Cras at nisl lorem, a lacin...",
      "InteractAction":"Morbi quis nunc in odio eg...",
      "InteractNotes":"Quisque et ante ut nis..."
   },

Or, were you trying to do this:

或者,你想这么做吗:

{
   "results": [
   {
      "InteractionID":"1",
      "AgentFullName":"Peter Germein",
      "InteractTopics":"Behaviour, Attendance, Attitude, Performance, Closing",
      "InteractDiscussion":"Cras at nisl lorem, a lacin...",
      "InteractAction":"Morbi quis nunc in odio eg...",
      "InteractNotes":"Quisque et ante ut nis..."
   },  
   {
      "InteractionID":"2",
      "AgentFullName":"Peter Germein",
      "InteractTopics":"Behaviour, Attendance, Attitude, Performance, Closing",
      "InteractDiscussion":"....",
      "InteractAction":"Morbi quis nunc in ...",
      "InteractNotes":"Quisque et ante ut nisi ..."
   },

Which would be parsed like this:

可以这样分析:

$.each(data.results, function(i, grab){
   alert(grab.AgentFullName);

})

#1


0  

[
   "results",
   {
      "InteractionID":"1",
      "AgentFullName":"Peter Germein",

referring the beginning of your json, you probably want to put "results":, instead of "results",

引用json的开头,您可能想要输入“results”:,而不是“results”,

#2


2  

The first element in your array is not an object, it's a string. That's why you're getting undefined for the first element when you say grab.AgentFullName—the string "results" has no such property.

数组中的第一个元素不是对象,而是一个字符串。这就是为什么当你说抓取时,第一个元素没有定义。agentfullname—字符串“结果”没有这样的属性。

You could change

你可以改变

[
   "results",
   {
      "InteractionID":"1",
      "AgentFullName":"Peter Germein",
      "InteractTopics":"Behaviour, Attendance, Attitude, Performance, Closing",
      "InteractDiscussion":"Cras at nisl lorem, a lacin...",
      "InteractAction":"Morbi quis nunc in odio eg...",
      "InteractNotes":"Quisque et ante ut nis..."
   },

to

[
   {
      "InteractionID":"1",
      "AgentFullName":"Peter Germein",
      "InteractTopics":"Behaviour, Attendance, Attitude, Performance, Closing",
      "InteractDiscussion":"Cras at nisl lorem, a lacin...",
      "InteractAction":"Morbi quis nunc in odio eg...",
      "InteractNotes":"Quisque et ante ut nis..."
   },

Or, were you trying to do this:

或者,你想这么做吗:

{
   "results": [
   {
      "InteractionID":"1",
      "AgentFullName":"Peter Germein",
      "InteractTopics":"Behaviour, Attendance, Attitude, Performance, Closing",
      "InteractDiscussion":"Cras at nisl lorem, a lacin...",
      "InteractAction":"Morbi quis nunc in odio eg...",
      "InteractNotes":"Quisque et ante ut nis..."
   },  
   {
      "InteractionID":"2",
      "AgentFullName":"Peter Germein",
      "InteractTopics":"Behaviour, Attendance, Attitude, Performance, Closing",
      "InteractDiscussion":"....",
      "InteractAction":"Morbi quis nunc in ...",
      "InteractNotes":"Quisque et ante ut nisi ..."
   },

Which would be parsed like this:

可以这样分析:

$.each(data.results, function(i, grab){
   alert(grab.AgentFullName);

})