weixin_33735676 2015-10-07 01:38 采纳率: 0%
浏览 16

Ajax表单仅适用于内联

So, I have following for ajax:

//Ajax 
    jQuery(document).ready(function() {
            jQuery('.my_popup_contact_open').click(function(e) {
                e.preventDefault();
                jQuery.ajax({
                    type: "GET",
                    url: "<?php echo admin_url('admin-ajax.php'); ?>", 
                    dataType: 'html',
                    data: ({ action: 'rh_contact_form_support'}),
                    success: function(data){
                          jQuery('.rhm_contact_support').html(data);                      
                },
                error: function(data)  
                {  
                alert("Error!");
                return false;
                }  
                }); 
         }); 
     }); 

For my my_js.js file, I have following setup:

//header script:
(function ($, root, undefined) {    
$(function () {     
    'use strict';

      //js goes here


    // Default line End
 });    
})(jQuery, this);

When I put the ajax js in the my_js.php, the function does not work. It only seems to work when I put the code inline at the bottom of php page.

Any suggestions to why it does not work when it is placed in "my_js.js"?

Thanks!

  • 写回答

2条回答 默认 最新

  • csdn产品小助手 2015-10-07 01:52
    关注

    Your problem is the following line in the $.ajax

     url: "<?php echo admin_url('admin-ajax.php'); ?>",
    

    It works inline because php will find and parse the <?php ?> tags in your HTML, generating the correct url but it won't parse tags in external javascript files so your url probably ends up unprocessed, performing ajax on an invalid URL and the server complains with a 505.

    To check if this is the case, open the browser terminal and search for said url in the external javascript file (under the sources tab if in chrome). If it's unprocessed, that's your culprit.

    评论

报告相同问题?