Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在ajax jquery中显示json响应,_Javascript_Php_Jquery_Json_Ajax - Fatal编程技术网

Javascript 在ajax jquery中显示json响应,

Javascript 在ajax jquery中显示json响应,,javascript,php,jquery,json,ajax,Javascript,Php,Jquery,Json,Ajax,我得到的JSON响应 { "user_data": { "id": 22, "first_name": xxx, "last_name": xxx, "phone_number": "123456789", }, "question_with_answers" : [ { "id": 1, "question

我得到的JSON响应

     {
        "user_data": {
        "id": 22,
        "first_name": xxx,
        "last_name": xxx,
        "phone_number": "123456789",
        },
        "question_with_answers" : [
            {
           "id": 1,
           "question_name": "Features of our project that you like (select one 
            or more):",
           "answers": [
                {
                "id": 19,
                "question_id": 1,
                "customer_id": 22,
                "option": "Location",
                },
                {
                "id": 20,
                "question_id": 1,
                "customer_id": 22,
                "option": "Architecture",
                 }, 
               ]
            },
            {
            "id": 2,
            "question_name": "Features of our project that you DID NOT like 
             (select one or more):",
                 "answers": [
                 {
                "id": 22,
                "question_id": 2,
                "customer_id": 22,
                "option": "Location",
                 },

                ]
             },
             {
             "id": 3,
             "question_name": "How soon are you looking to buy a new home?",
             "answers": [
                    {
                       "id": 24,
                       "question_id": 3,
                       "customer_id": 22,
                       "option": "Immediately",
                   }
                 ]
              }
           ]
          }
这就是JSON响应的样子,现在我需要使用jquery显示数据

我的js代码

     function openModal(Id){
     var CustomerId = Id;
     $.ajax({
        type : 'get',
        url: 'customer-details',
        data: {
            'CustomerId':CustomerId
        },

        success: function(data)
        {
            //what I have to do here
        }
     })
} 
我想要的输出是

first_name : xxx           
last_name  : xxx 
phone_number : 123456789

1.Features of our project that you like (select one or more):
ans: Location, Architecture

2.Features of our project that you DID NOT like (select one or more)
ans: Location

3.How soon are you looking to buy a new home?
ans: Immediately
这就是从上面的json响应中我的输出应该是什么样子的,是否可以使用我得到的json响应获得上面的输出
我已经从后端传递了两个变量,比如user_data和question_以及答案

您必须在$.ajax调用中设置数据类型,将其设置为数据类型:“json”

之后,在json对象的success中使用数据变量,可以像data.user\u data或data.id或data.first\u name那样访问它

如果不将json定义为数据类型,它将无法正常工作

附加 如果你想显示“带答案的问题”的内容,你必须重复它,比如


for(i in data.question_with_answer){alert(data.qestion_with_answer[i])}

在成功处理程序中,首先使用

var obj=JSON.parse(数据)

现在在数据中,您有了响应的数组,您可以像这样使用它

obj.first\u name

obj.姓氏

注意:-可能您在
JSON.parse中有问题,所以请先使用

var data=json.stringify(数据)

然后使用Json.parse函数并使用上述数据变量
这个数据

你能用javascript或jquery向页面添加html元素吗?是的,我能@marzelinhello,@rebru我能显示第一个对象用户的数据,但其中有一个数组,这让我感到困惑。我怎么能用数组值显示问题?我编辑了我的第一篇文章,对于对象中的数组,您必须通过它进行迭代。第二个变量数据是什么?有两个变量,请使用lookvar data=JSON.parse(data);这里的数据是从php获得的json,并传入成功处理程序,有时会给出错误,因为json转换为字符串是的,我知道josn是字符串,但json有一种特殊的格式,所以如果遇到错误,它会给出错误,然后使用
var data=json.stringify(数据)
在json.parse函数和stringify函数之前,您传递成功处理程序中使用的json可能是您得到了我的观点这两个数据都是sameI think obj.user_data.first_name解析后可以工作。@falero80s:-是的,您是对的,用户_数据从我的眼睛中跳过,感谢指出