jquery - Ajax post request doesn't work in wp theme directory -
this example of ajax post request. work when placed outside of wordpress active theme directory. when there , norefresh.php uploaded there doesn't work no matter exact patch norefresh.php use (site/themefolder/norefresh.php or server patch or local patch norefresh.php or /norefresh.php). doesn't work @ all.
is there wordpress prevents execution.what should do?
$.ajax({ type: "post", url: "norefresh.php", data: reqdata, cache: false, success: function(html) { //document.getelementbyid('mytextarea').value = html; alert(html); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
you can allow access using htaccess specific file, pain clients / if move sites etc?
better hook function either plugin or theme. don't need worry relative uris. (some minor differences in js other virtually same)
add_action( 'wp_ajax_custom_hook', 'custom_function' ); add_action( 'wp_ajax_nopriv_custom_hook', 'custom_function' ); //-->front end hook... function custom_function(){ // handle code... } add_action('wp_head','define_ajaxurl'); //--> define ajaxurl using php can neatly place js in js file function define_ajaxurl() { ?> <script type="text/javascript"> var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>'; </script> <?php }
js
$.ajax({ type: "post", url: ajaxurl, //-->see php function hooked wphead define this!! //data: reqdata, //--> need pass action....which hook!! data: { action: 'custom_hook', //-->name of hook used above..ie. wp_ajax_nameofhook... 'otherfield': 'yea' } cache: false, success: function(html) { //document.getelementbyid('mytextarea').value = html; alert(html); } });
Comments
Post a Comment