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

时间:2014-02-11 19:43:52

标签: javascript php jquery

我想将数据从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'

那么代码中的错误在哪里?还有更好的方法来完成这项任务吗?

1 个答案:

答案 0 :(得分:0)

我认为@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
    }
});
相关问题