Retrieving json data from php in another domain in AJAX in WordPress

Asked 1 years ago, Updated 1 years ago, 88 views

I would like to retrieve data from AJAX from different php domains (in JSON format).
If you type https:// jp /.jp/test.php?des=50 in the browser, JSON data will be displayed.
I would like to send this des=50 in POST format, not in GET format, but the value entered by the user from the browser.
I have written the following code in function.php, but after success, I get a false message.

false.

Below is what function.php describes:

functionajax_scripts(){
  $handle='tour_main';
  $file=get_template_directory_uri().'/js/'.$handle.'.js';
  wp_register_script($handle, $file, array('jquery'));

  $action='tourajax-action';

  wp_localize_script($handle, 'TOURAJAX_AJAX', [
      'api' = > admin_url('admin-ajax.php'),
      'action' = > $action,
      'nonce' = > wp_create_nonce($action)
  ]);
  wp_enqueue_script($handle);
}
add_action('wp_enqueue_scripts', 'ajax_scripts');


function ajax_tourajax_call(){
  $action='tourajax-action';
  $data=';

  if(check_ajax_refer($action, 'nonce', false){
        $data=@file_get_contents("https:// 〇 ..jp/test.php?des=50");
        if(is_wp_error($data)){
            echo$data->get_error_message();
            exit;
        }
        else{
            wp_send_json($data);
        }

  } else{
    status_header('403');
    $data='Forbidden';
  }
  header('Content-Type:application/json; charset=UTF-8');
  echo_send_json($data);

  //die(); Do you need this??
}
add_action('wp_ajax_tourajax-action', 'ajax_tourajax_call');
add_action('wp_ajax_nopriv_tourajax-action', 'ajax_tourajax_call');

Below is what I wrote in js.

jQuery("#testButton").click(function(){
        varJSONdata={
            action —TOURAJAX_AJAX.action,
            nonce —TOURAJAX_AJAX.nonce,
            des: jQuery("#des").val()
        };

    alert(JSON.stringify(JSONdata);

    jQuery.ajax({
        url —TOURAJAX_AJAX.api,
        type: "POST",
        dataType: 'json',
        data —JSONdata,
        scriptCharset: 'utf-8',
        success:function(data){

            // Success
            alert("success");
            alert(data);
            jQuery("#response").html(JSON.stringify(data)));
        },
        error: function(data){

            // Error
            alert("error");
            alert(JSON.stringify(data)));
            jQuery("#response").html(JSON.stringify(data)));
        }
    });
})

Please tell me who it is.

php wordpress ajax

2022-09-30 19:26

2 Answers

file_get_contents returns false on error.Of course, it is not WP_Error, so is_wp_error is useless.(Also, I think the false that came back here is the response.)

HTTP requests with file_get_contents can be inconvenient.We recommend that you use wp_remote_request(), wp_remote_post() to make external requests within WP.See WP_Http::request() for arguments.


2022-09-30 19:26

wp_send_json(); is

Encoded as JSON, printed and die

Therefore, the response body will be sent out at this point.
Therefore, header('Content-Type:application/json; charset=UTF-8'); must run earlier, and echo wp_send_json($data); is not required.

Note: https://wpdocs.osdn.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/wp_send_json


2022-09-30 19:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.