Javascript jQuery Post内部工作

Javascript jQuery Post内部工作,javascript,php,jquery,Javascript,Php,Jquery,最近,我一直在努力摆脱使用jQuery来完成每一项小任务,因为我需要做的大部分时间都是提交表单,然而,当尝试运行一个简单的POST请求时,我注意到它实际上并没有被发送到POST变量,因为在使用以下方法提交时转储它必须使用file\u get\u contents('php://input") 但是,在jQuery中使用内置的ajax post将数据发送到post变量,并且可以在我的php脚本中使用$\u post检索数据。有谁能给我解释一下这个函数的内部工作原理,以及它们是如何实现的 谢谢我想您

最近,我一直在努力摆脱使用jQuery来完成每一项小任务,因为我需要做的大部分时间都是提交表单,然而,当尝试运行一个简单的POST请求时,我注意到它实际上并没有被发送到POST变量,因为在使用以下方法提交时转储它必须使用file\u get\u contents('php://input")

但是,在jQuery中使用内置的ajax post将数据发送到post变量,并且可以在我的php脚本中使用$\u post检索数据。有谁能给我解释一下这个函数的内部工作原理,以及它们是如何实现的


谢谢

我想您感兴趣的部分是
ajax
函数的这个片段,如果传递的数据不是字符串,它将调用
jQuery.param()

// Convert data if not already a string
if ( s.data && s.processData && typeof s.data !== "string" ) {
    s.data = jQuery.param( s.data, s.traditional );
}
jQuery.param()
,它将两种格式(包括javascript对象)转换为
key1=value1&key2=value2
格式的查询字符串:

// Serialize an array of form elements or a set of
// key/values into a query string
jQuery.param = function( a, traditional ) {
    var prefix,
        s = [],
        add = function( key, value ) {
            // If value is a function, invoke it and return its value
            value = jQuery.isFunction( value ) ? value() : ( value == null ? "" : value );
            s[ s.length ] = encodeURIComponent( key ) + "=" + encodeURIComponent( value );
        };

    // Set traditional to true for jQuery <= 1.3.2 behavior.
    if ( traditional === undefined ) {
        traditional = jQuery.ajaxSettings && jQuery.ajaxSettings.traditional;
    }

    // If an array was passed in, assume that it is an array of form elements.
    if ( jQuery.isArray( a ) || ( a.jquery && !jQuery.isPlainObject( a ) ) ) {
        // Serialize the form elements
        jQuery.each( a, function() {
            add( this.name, this.value );
        });

    } else {
        // If traditional, encode the "old" way (the way 1.3.2 or older
        // did it), otherwise encode params recursively.
        for ( prefix in a ) {
            buildParams( prefix, a[ prefix ], traditional, add );
        }
    }

    // Return the resulting serialization
    return s.join( "&" ).replace( r20, "+" );
};
//序列化表单元素数组或一组
//键/值转换为查询字符串
jQuery.param=函数(a,传统){
变量前缀,
s=[],
add=函数(键、值){
//如果value是函数,则调用它并返回其值
value=jQuery.isFunction(value)?value():(value==null?”:value);
s[s.length]=encodeURIComponent(键)+“=”+encodeURIComponent(值);
};

//将jQuery@AnandSomasekhar不使用w3school的traditional设置为trueplease@AnandSomasekhar这根本不能回答我的问题。请阅读它。你到w3schools的链接很有趣。@mschuett如果你想看看jQuery是如何做事情的,请查看源代码。“我一直在努力避免在每一个小任务中使用jQuery”-简化ajax可能是使用jQuery的最佳理由。这是浏览jQuery源代码的一种方便方式-它在函数定义之间有超链接,因此您不必上下滚动或手动搜索。我知道内部工作都在ajax调用中。虽然我可以编写一些javascript,但我并不了解d可怕的ajax函数的内部工作。
// Serialize an array of form elements or a set of
// key/values into a query string
jQuery.param = function( a, traditional ) {
    var prefix,
        s = [],
        add = function( key, value ) {
            // If value is a function, invoke it and return its value
            value = jQuery.isFunction( value ) ? value() : ( value == null ? "" : value );
            s[ s.length ] = encodeURIComponent( key ) + "=" + encodeURIComponent( value );
        };

    // Set traditional to true for jQuery <= 1.3.2 behavior.
    if ( traditional === undefined ) {
        traditional = jQuery.ajaxSettings && jQuery.ajaxSettings.traditional;
    }

    // If an array was passed in, assume that it is an array of form elements.
    if ( jQuery.isArray( a ) || ( a.jquery && !jQuery.isPlainObject( a ) ) ) {
        // Serialize the form elements
        jQuery.each( a, function() {
            add( this.name, this.value );
        });

    } else {
        // If traditional, encode the "old" way (the way 1.3.2 or older
        // did it), otherwise encode params recursively.
        for ( prefix in a ) {
            buildParams( prefix, a[ prefix ], traditional, add );
        }
    }

    // Return the resulting serialization
    return s.join( "&" ).replace( r20, "+" );
};