php - Ajax call to my Wordpress website from an external application

This question already exists:AJAX request from Chrome Extension to Wordpress WebsiteClosed 5 years ago.I have on website

This question already exists: AJAX request from Chrome Extension to Wordpress Website Closed 5 years ago.

I have on website on wordpress and a external web app. I try to make an AJAX call from this external web app to get some info from my wordpress site/ database but i got an error message 400.

Here is my ajax call code :


        jQuery.ajax({

        type:"POST",
        url: ".php",
        contentType: 'json',
        dataType: "JSON",
        responseType:'json',

        data: {
            action : "itempricingfunction",
            ean : "EANTEST0101010"
        },

        success:function(data){

            alert(data);

        },

        error: function(errorThrown){
            console.log(errorThrown);
        }
        });

and here is my functions.php file inside my wordpress website :

function itempricingfunction(){


$product_ean = (isset($_POST['ean'])) ? htmlentities($_POST['ean']) : NULL;
echo json_encode(array('product_ean' =>$product_ean));


    exit;
}

add_action("wp_ajax_nopriv_itempricingfunction", "itempricingfunction");
add_action("wp_ajax_itempricingfunction", "itempricingfunction");

The response from wordpress is error 404.

Can you help me to communicate and set/get some data from my worpdress website please ?

Thank you in advance.

This question already exists: AJAX request from Chrome Extension to Wordpress Website Closed 5 years ago.

I have on website on wordpress and a external web app. I try to make an AJAX call from this external web app to get some info from my wordpress site/ database but i got an error message 400.

Here is my ajax call code :


        jQuery.ajax({

        type:"POST",
        url: "https://www.groupio.fr/wp-admin/admin-ajax.php",
        contentType: 'json',
        dataType: "JSON",
        responseType:'json',

        data: {
            action : "itempricingfunction",
            ean : "EANTEST0101010"
        },

        success:function(data){

            alert(data);

        },

        error: function(errorThrown){
            console.log(errorThrown);
        }
        });

and here is my functions.php file inside my wordpress website :

function itempricingfunction(){


$product_ean = (isset($_POST['ean'])) ? htmlentities($_POST['ean']) : NULL;
echo json_encode(array('product_ean' =>$product_ean));


    exit;
}

add_action("wp_ajax_nopriv_itempricingfunction", "itempricingfunction");
add_action("wp_ajax_itempricingfunction", "itempricingfunction");

The response from wordpress is error 404.

Can you help me to communicate and set/get some data from my worpdress website please ?

Thank you in advance.

Share Improve this question edited Jan 20, 2020 at 20:52 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Jan 20, 2020 at 20:33 Tayfun AkaltunTayfun Akaltun 11 silver badge3 bronze badges 2
  • Does this answer your question? AJAX request from Chrome Extension to Wordpress Website – Antti Koskinen Commented Jan 21, 2020 at 7:15
  • Is the error 400 or 404? You've said both, but they're different errors that mean different things. It's important to be precise. – Jacob Peattie Commented Jan 21, 2020 at 7:31
Add a comment  | 

1 Answer 1

Reset to default 0

You shouldn't be sending your data as JSON:

dataType: "JSON"

For add_action("wp_ajax_nopriv_itempricingfunction" to work, WordPress needs to find the action parameter in your request, which you're correctly setting here:

    data: {
        action : "itempricingfunction",
        ean : "EANTEST0101010"
    },

The problem is that it checks this using $_REQUEST['action'], but PHP doesn't populate $_REQUEST with JSON. It only works with form data. So remove the dataType line and you should be fine:

jQuery.ajax(
    {
        type: 'POST',
        url: 'https://www.groupio.fr/wp-admin/admin-ajax.php',
        responseType: 'json',
        data: {
            action: 'itempricingfunction',
            ean: 'EANTEST0101010'
        },
        success: function( data ) {
            alert( data );
        },
        error: function( errorThrown ) {
            console.log( errorThrown );
        }
    }
);

There's a couple of other things to consider too:

  • If the web app is on a different domain, you may run into CORS issues.
  • A custom REST API endpoint would be more appropriate for this sort of thing, and does support receiving data as JSON.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744816677a4595367.html

相关推荐

  • php - Ajax call to my Wordpress website from an external application

    This question already exists:AJAX request from Chrome Extension to Wordpress WebsiteClosed 5 years ago.I have on website

    2天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信