简体   繁体   English

jQuery将POST参数发送到PHP

[英]jQuery send POST parameters to PHP

This is how I'm trying to send some data to a php file: 这就是我试图将一些数据发送到php文件的方式:

function requestWsList(functionToProcess, me) {
    jQuery.support.cors = true;


    $.ajax({
        crossDomain: true,
        traditional: true,
        url: urlWS(),
        contentType: "text/xml;charset=UTF-8",
        dataType: "html",  
        type: "POST",
        data: {name: 'value', anotherName: 'another value'},
        processData: false,
        success:function(data) {
            functionToProcess(data, me);
        } });

}

When I call this javascript function, the php request is triggered but I always get an empty array in the php variable $_POST. 当我调用此javascript函数时,会触发php请求,但我总是在php变量$ _POST中得到一个空数组。

What am I doing wrong? 我究竟做错了什么? What do I have to do to read the "name" and "anotherName" variables in PHP? 要读取PHP中的“名称”和“ anotherName”变量,该怎么办?

Try to convert your JSON object into a string. 尝试将JSON对象转换为字符串。 For example: 例如:

data: '{ "name": "value", "anotherName": "another value" }

If you're using a browser that supports it you can use JSON.stringify() 如果您使用的浏览器支持它,则可以使用JSON.stringify()

For example: 例如:

data: JSON.stringify({name: 'value', anotherName: 'another value'});

You set contentType to "text/xml;charset=UTF-8" and you are passing a JavaScript object. 您将contentType设置为"text/xml;charset=UTF-8"并且正在传递JavaScript对象。 Just remove this parameter and it should work. 只需删除此参数,它应该可以工作。

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

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