简体   繁体   English

Wordpress / Jquery Ajax,获取工作后发布失败并出现jquery错误

[英]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? 大家好,以下所有功能都可以正常运行,并且完全按照我的意愿运行,但是我希望这是一个.post而不是.get。有人可以看到以下问题吗? 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: 我将其设置为.post时遇到的错误是:

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

Which is line 2963 of an un-minified version of jquery 这是未缩小版本的jquery的第2963行

/*! 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, 我正在使用最新版本的Firefox,

I got the following returned as an errotThrowen['error'] 我得到了以下内容作为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 并在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. 如果我将安全性遗漏在url中,它将失败,我不知道为什么,但这使我绕了几个小时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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