Access json data from php

前端 未结 3 1866
野性不改
野性不改 2021-01-17 00:32

I have a problem accessing JSON data. I\'m new to JSON and jquery so there is probably a easy solution to it and I would be glad to find out.

My jQuery:



        
3条回答
  •  被撕碎了的回忆
    2021-01-17 01:18

    In JQuery, you need to set the return data type (dataType) to json so the function knows what type of data to expect and process. From the manual:

    "json": Evaluates the response as JSON and returns a JavaScript object. In jQuery 1.4 the JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. (See json.org for more information on proper JSON formatting.)

    You can do this with the full $.ajax() call, or you can use $.getJSON(). There is no HTTP POST shortcut to return JSON (i.e. $.postJSON doesn't exist), but you can supply the dataType parameter to $.ajax() or just add the parameter to $.post() . When you have a JSON object, use json.keyName to access the data.

    $.ajax({
        url: "currentPage.php",
        data: { 
            'currentPage': 1
        },
        dataType: "json",
        type: "post",
        success: function(data) {
            $("body").append(data);  
        }
    });
    

提交回复
热议问题