繁体   English   中英

如何将数据从js发送到php,反之亦然?

[英]how to send data from js to php and vice versa?

我想将数据从js发送到php,然后将它们重新发送到js以在函数中使用它们,这是我的代码:

$.ajax({
    type:"POST",
    url: "pages/quantity.php",
    data: product,
    success: function(){
    jQuery.extend({
        getValues: function(url) {
            var result = null;

            $.ajax({
                url: url,
                type: 'get',
                dataType: 'json',
                async: false,
                success: function(data) {
                   result = JSON.stringify(data);
                }
            });

             return result;
          }
       });
    }
});



var q = document.qntfrm.qnt.value;

var max = $.getValues("pages/quantity.php");
alert(max);

但是我得到了这个错误:

Uncaught TypeError: Object function ( selector, context ) {
        // The jQuery object is actually just the init constructor 'enhanced'
        return new jQuery.fn.init( selector, context );
    } has no method 'getValues'

那么我的代码中的错误在哪里呢? 有没有更好的方法来执行此任务?

我认为@Moob是正确的。

您为什么不尝试以下方法:

var obj = {};
obj.name_of_parameter = val
// val is your param ...
// and name_of_parameter as to match your php get or post variable name

var str = jQuery.param(obj);

$.ajax({
    type: "POST",
    url: 'the_url',
    dataType: 'json',
    data: str,
    success: function (data) {
            // code if ok
    },

    error: function() {
        // code if something went wrong
    }
});

暂无
暂无

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

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