将How to retrieve and display JSON content in a WordPress function using PHP?
P粉731861241
P粉731861241 2024-03-30 17:42:32
0
1
442

I have a wordpress endpoint and have some json data. Unfortunately I don't know how to return this json data in the function. I tried json_decode but it returned nothing. Then the endpoint will work. If I use json_encode it returns the data but also includes newlines and other stuff. The problem seems to be with the syntax, since it's already a complete json that I have. How to return something already in json syntax?

add_action('wp_ajax_nopriv_inboundCall', 'testFunction');
add_action('wp_ajax_inboundCall', 'testFunction');
 
function testFunction() {
    
    echo json_decode('{
        "testData": [
          {
            "_id": "1",
            "name": "testName1"
          },
          {
            "_id": "2",
            "name": "testName2"
          },
        ],
        "testState": {
          "1": [
            1,
            0
          "2": [
            1,
            0
          ]
         }
       }'); 

      die(); 
}

P粉731861241
P粉731861241

reply all(1)
P粉980815259
function testFunction() {
return json_decode('{
    "testData": [
      {
        "_id": "1",
        "name": "testName1"
      },
      {
        "_id": "2",
        "name": "testName2"
      },
    ],
    "testState": {
      "1": [
        1,
        0
      "2": [
        1,
        0
      ]
     }
   }');  }
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template