admin管理员组文章数量:1122846
I wrote a plugin that calls a PHP function from JQuery and the php sends a json response back to the Jquery using wp_send_json()
. The functions are all called successfully, but the json request sends a lot of html to the jquery function every time. How do I make the json sent by the php function only a specific message?
JQUERY:
jQuery( document ).ready( function() {
jQuery( 'body' ).on( 'click', '.wpm_mail_link', function( e ) {
var varData = 'name:foobar&[email protected]';
jQuery.ajax({
type: "POST",
action: "wp_ajax_send_email",
url: my_ajax_obj.ajax_url,
data: varData,
success: function(data) {
console.log('Ajax request successful');
console.log(data.message);
}
});
});
});
PHP:
public function send_email() {
$response = array(
'message' => 'Sent',
'ID' => 1,
);
wp_send_json( $response );
}
I wrote a plugin that calls a PHP function from JQuery and the php sends a json response back to the Jquery using wp_send_json()
. The functions are all called successfully, but the json request sends a lot of html to the jquery function every time. How do I make the json sent by the php function only a specific message?
JQUERY:
jQuery( document ).ready( function() {
jQuery( 'body' ).on( 'click', '.wpm_mail_link', function( e ) {
var varData = 'name:foobar&[email protected]';
jQuery.ajax({
type: "POST",
action: "wp_ajax_send_email",
url: my_ajax_obj.ajax_url,
data: varData,
success: function(data) {
console.log('Ajax request successful');
console.log(data.message);
}
});
});
});
PHP:
public function send_email() {
$response = array(
'message' => 'Sent',
'ID' => 1,
);
wp_send_json( $response );
}
Share
Improve this question
asked Apr 13, 2019 at 0:00
brothman01brothman01
1941 silver badge11 bronze badges
5
|
1 Answer
Reset to default 0Fixed it! I was getting a false positive with the ajax request. I thought it was going through but returning 'undefined' but I was actually using the wrong url.
本文标签: AJAX in plugin wpsendjson() sending html
版权声明:本文标题:AJAX in plugin wp_send_json() sending html 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736285665a1927506.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
e.preventDefault()
in your click function in js. – MikeNGarrett Commented Apr 13, 2019 at 0:41