简体   繁体   中英

Wordpress / Jquery ajax, get works post fails with jquery error

Hi all the following function will work and do exactly as I want it to but I want this to be a .post not a .get can anyone see a problem with the following? its pretty much straight from another answer on stack overflow and should work fine.

jQuery(document).ready(function() {
        //This function adds a development.
        jQuery('#add_dev').bind('submit', function(e) {
        e.preventDefault();
        var data = {
            action: 'AjaxAddDev',
            security: AjaxHandler.ajaxnonce,
            name: jQuery('#dev_name').val(),
            desc: jQuery('#dev_desc').val()
        };        
        //alert(data['name']+data['desc']);
        jQuery.get(
                AjaxHandler.ajaxurl,
                data,
                function(response) {
                    // ERROR HANDLING
                    if (!response.success) {
                        // No data came back, maybe a security error
                        if (!response.data) {
                            //$('#my-answer').html('AJAX ERROR: no response');
                            alert("Problem adding Development");
                        } else {
                            //$('#my-answer').html(response.data.error);
                            alert(response.data);
                        }
                    } else {
                        //$('#my-answer').html(response.data);
                        alert("Problem adding Development");
                    }
                }
        );
    });
});

The error I get when I set it to .post is:

l.send(n.hasContent && n.data || null), r = function (e, i) {

Which is line 2963 of an un-minified version of jquery

/*! jQuery v1.10.2 | (c) 2005, 2013 jQuery Foundation, Inc. | jquery.org/license */ 

Can anyone point me in the right Direction?

Updated Code:

jQuery(document).ready(function() {
        //This function adds a development.
        jQuery('#add_dev').bind('submit', function(e) {
        e.preventDefault();
        var data = {
            action: 'AjaxAddDev',
            security: AjaxHandler.ajaxnonce,
            name: jQuery('#dev_name').val(),
            desc: jQuery('#dev_desc').val()
        };        
        //alert(data['name']+data['desc']);
        jQuery.ajax({
            url: AjaxHandler.ajaxurl,
            type: "POST",
            data: data,
            success:function(data) {
            // This outputs the result of the ajax request
                alert(data);
            },
            error: function(errorThrown){
                alert(errorThrown['error']);
            }
        });
    });
});

I am using firefox latest version,

I got the following returned as an errotThrowen['error']

function () {
                if (l) {
                    var t = l.length;
                    (function i(t) {
                        x.each(t, function (t, n) {
                            var r = x.type(n);
                            "function" === r ? e.unique && p.has(n) || l.push(n) : n && n.length && "string" !== r && i(n)
                        })
                    })(arguments), n ? o = l.length : r && (s = t, c(r))
                }
                return this
            }

if you want to ajax on change

$("#yourid").change(function () {            
        var p = {
            postfieldname: value,
            postfieldname: value,
            postfieldname: value,
            postfieldname: value,
            postfieldname: value,
            postfieldname: value,
            postfieldname: value,
             }
        $.ajax({
            url: "library/test.php",
            type: "POST",
            data: p,
            success: function (e) {
                var t = jQuery.parseJSON(e);
                $("#id").val(t['a']);
            }
        })
    })

and on test.php

$array = array("a" => "test", "b" => "array");
$encode = json_encode($aray);
echo $encode;

OK this was kind of an odd one,

To get it working I simply had to add the following as the post URL.

url: AjaxHandler.ajaxurl+"&security="+AjaxHandler.ajaxnonce,

If I left the security out of the url it would fail, I don't know why but this had me going around in circles for hours.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM