admin管理员组文章数量:1392088
this seems hard to understand. I created a plugin, and it works fine on different installations of me and a few other blogs.
But on one blog of someone else it fails. After getting through debugs and console entries, I believe I found the source of the failure:
At some step of the process the plugin calls a function with ajax:
jQuery(document).ready(function($) {
var data = {
'action': 'mp_throwcontent_2',
'MedioPay_postid': mp_mypostid,
'MedioPay_outputs': mp_outputs,
'MedioPay_number': mp_numberof_payments,
'MedioPay_userID': mp_userID,
'Mediopay_newCounter': mp_newCounter,
'MedioPay_firstPartner': mp_firstPartner,
'MedioPay_secondPartner': mp_secondPartner,
'MedioPay_thirdPartner': mp_thirdPartner,
'MedioPay_fourthPartner': mp_fourthPartner,
'MedioPay_shareQuote': mp_sharing,
'MedioPay_preview': mp_preview
};
console.log("turning data over");
jQuery.post(my_ajax_object.ajax_url, data, function(response) {
console.log("unlock 2 " + response);
mp_unlockContent2(payment, response);
});
});
Then a function is called:
add_action ( 'wp_ajax_nopriv_mp_throwcontent_2', 'mp_throwcontent_2' );
function mp_throwcontent_2() {
// some ifs and so on
// echo "output"
}
When the plugin starts, Ajax is localized
wp_localize_script( 'ajax-script', 'my_ajax_object',
array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
A little quirk: I localized it twice, accidently, here too:
wp_localize_script( 'ajax-script', 'ajax_object',
array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );
Can this be a problem?
All this works very fine on a few blogs. But in one the operations just stop when the ajax starts. No error, nothing in debug, it just stops.
Does anybody have an idea what I can do to make it run? Is there a fallback to call in case it fails?
Ah, and to make it even weirder: The plugin worked fine on that blog, but suddenly it stopped working, while the owners tells me he didn't even change anything on his wordpress installation.
this seems hard to understand. I created a plugin, and it works fine on different installations of me and a few other blogs.
But on one blog of someone else it fails. After getting through debugs and console entries, I believe I found the source of the failure:
At some step of the process the plugin calls a function with ajax:
jQuery(document).ready(function($) {
var data = {
'action': 'mp_throwcontent_2',
'MedioPay_postid': mp_mypostid,
'MedioPay_outputs': mp_outputs,
'MedioPay_number': mp_numberof_payments,
'MedioPay_userID': mp_userID,
'Mediopay_newCounter': mp_newCounter,
'MedioPay_firstPartner': mp_firstPartner,
'MedioPay_secondPartner': mp_secondPartner,
'MedioPay_thirdPartner': mp_thirdPartner,
'MedioPay_fourthPartner': mp_fourthPartner,
'MedioPay_shareQuote': mp_sharing,
'MedioPay_preview': mp_preview
};
console.log("turning data over");
jQuery.post(my_ajax_object.ajax_url, data, function(response) {
console.log("unlock 2 " + response);
mp_unlockContent2(payment, response);
});
});
Then a function is called:
add_action ( 'wp_ajax_nopriv_mp_throwcontent_2', 'mp_throwcontent_2' );
function mp_throwcontent_2() {
// some ifs and so on
// echo "output"
}
When the plugin starts, Ajax is localized
wp_localize_script( 'ajax-script', 'my_ajax_object',
array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
A little quirk: I localized it twice, accidently, here too:
wp_localize_script( 'ajax-script', 'ajax_object',
array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );
Can this be a problem?
All this works very fine on a few blogs. But in one the operations just stop when the ajax starts. No error, nothing in debug, it just stops.
Does anybody have an idea what I can do to make it run? Is there a fallback to call in case it fails?
Ah, and to make it even weirder: The plugin worked fine on that blog, but suddenly it stopped working, while the owners tells me he didn't even change anything on his wordpress installation.
Share Improve this question edited Feb 2, 2020 at 22:49 Christoph Bergmann asked Feb 2, 2020 at 22:26 Christoph BergmannChristoph Bergmann 1011 bronze badge 3 |2 Answers
Reset to default 0Are you logged into one of the sites? wp_ajax_nopriv_
hooks will not fire if the user is logged in, so if you've only used that hook, the AJAX request will not work at all if you are logged in.
You will need to hook both wp_ajax_nopriv_mp_throwcontent_2
and wp_ajax_mp_throwcontent_2
for it to work with both logged in and logged out users.
Byusing the ancient admin AJAX API, the only error information you can get from the response is either a HTTP error code, or a 0
, neither of which are particularly helpful.
So first, lets actually handle the failure in jQuery so we know when it's failed:
jQuery.post(my_ajax_object.ajax_url, data, function(response) {
console.log("unlock 2 " + response);
mp_unlockContent2(payment, response);
}).fail( function( jqXHR, textStatus, errorThrown) {
// it failed!
console.log( errorThrown );
} );
Now we can see what the AJAX request contained if it failed
Next, lets swap this:
add_action ( 'wp_ajax_nopriv_mp_throwcontent_2', 'mp_throwcontent_2' );
function mp_throwcontent_2() {
// some ifs and so on
// echo "output"
}
For this:
add_action( 'rest_api_init', 'add_custom_users_api');
function add_custom_users_api(){
register_rest_route( 'bergmann/v1', '/throwcontent', array(
'methods' => 'POST',
'callback' => 'mp_throwcontent_2',
));
}
function mp_throwcontent_2( $request ) {
$response = '';
// use $request['stuff'] instead of $_POST['stuff']
// otherwise put all the relevant stuff in $response
return $response;
}
Now we have a URL at yoursite/wp-json/bergmann/v1/throwcontent
, and if we make a GET
request ( e.g. with jQuery.get
or visiting it in a browser ), we'll get $response
in JSON form.
So lets updatethe localized script:
wp_localize_script( 'ajax-script', 'ajax_object',
[
'endpoint_url' => rest_url( '/bergmann/v1/throwcontent' ),
'we_value' => 1234
]
);
Then update our JS code:
jQuery.post(
my_ajax_object.endpoint_url,
data,
function(response) {
const reply = JSON.parse( response );
console.log("unlock 2 " + reply );
mp_unlockContent2(payment, reply);
}
).fail( function( jqXHR, textStatus, errorThrown) {
// it failed!
console.log( errorThrown );
} );
Some notes:
- Indent indent indent, and be consistent, a good editor will do it for you, and it avoids so many types of bug
- If you're getting stuff,
GET
stuff, don'tPOST
unless you're sending data - Admin AJAX is old, very old, if you make a mistake in the REST API it will just tell you in english in the response. E.g. if you request an endpoint that doesn't exist, it says so, but Admin AJAX will give you a
0
mystery reply - Your original problem is probably related to only doing
nopriv
, if you'd used the REST API it would have just told you that, rather than making you guess - If you want parameters to send, you can add them during
register_rest_route
, and WP will do all the checking and sanitising and validating for you - Ideally you want to ditch
jQuery
in favour of the more modernfetch
and a polyfill for old browsers, or at leastjQuery.ajax
- Always handle the errors, don't just make the request then scratch your head when it doesn't succeed, add the failure/error handlers! The worst that happens is you get a helpful error message
本文标签: functionsAjax in plugin failsbut only on one blogno idea why
版权声明:本文标题:functions - Ajax in plugin fails - but only on one blog - no idea why 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744781249a2624727.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
jQuery.post
has no failure/error handler, can you add one to see what the error was so we're not guessing? I also saw you're using the old admin ajax handler for AJAX, rather than making AJAX requests to the modern more reliable REST API, was there a specific reason? Note that you'll need to consult the jQuery docs for how to handle failures when making HTTP requests. Did you check your PHP error log? Also is the user logged in? If this were the REST API it would just tell you the issue in an error message in the response, but Admin AJAX can be cryptic – Tom J Nowell ♦ Commented Feb 3, 2020 at 0:01wp-admin/admin-ajax.php
you POST to/wp-json/yournamespace/v1/yourendpoint
, then in the handler you return rather than echo the response, look up theregister_rest_route
function – Tom J Nowell ♦ Commented Feb 3, 2020 at 12:19