dougu7546 2013-12-13 20:56
浏览 101
已采纳

Ajax post-POST数组在服务器端返回空

I have an js function that is collecting data and sending it to a php file.

I am trying to submit an array as part of the post:

function send_registration_data(){

var string = "{username : " + $('#username').val() + ", password : " + $('#pass1').val() + ", level : " + $("#userrole").val() + ", 'property[]' : [";
var c = 0;
$('input[name=property]:checked').each(function(){
    if( c == 0){
        string +="\"" +this.value+"\"";
        c=1;
    } else {
        string +=",\""+this.value+"\"";
    }
});
string+="]}";
$('#input').html( JSON.stringify(eval("(" + string + ")")) );

$.ajax({ url: './php/submit_registration.php',
         //data: { username : $('#username').val() , password : $('#pass1').val() , email : $('#email').val() , level : $("#userrole").val() },
         data: JSON.stringify(eval("(" + string + ")")) ,
         type: 'post',
         success: function(output) {
                  $('#output').html( output );

            }
});
};

On submit my php file returns an the POST array as NULL. I am not sure what I am doing wrong here.

EDIT: IT is the same weather I try to convert the string to json or not.

ALso, the inputs contain just text names.

  • 写回答

2条回答 默认 最新

  • dqt83336 2013-12-13 21:22
    关注

    string keyword

    Do not use the "string" keyword.

    eval

    Eval is evil - use it with caution.

    strict mode

    Make sure always to work in the "strict mode" by placing this line at the beginning of your code:

    'use strict'
    

    Building your response object

    You do not have to glue your post object manually. Just do it this way:

    var post = {
        'username': $('#username').val(),
        'password': $('#password').val(),
        'myArray[]': ['item1', 'item2', 'item3']
    };
    

    jQuery the right way

    Avoid messing up with unnecessary syntax.

    $.post(url, post)
        .done(function(response){
            // your callback
        });
    

    Conclusion

    'use strict'
    var url = './php/submit_registration.php'; // try to use an absolute url
    var properties = {};
    $('input[name="property"]:checked').each(function() {
        properties.push(this.value);
    });
    var data = {
        'username':   $('#username').val(),
        'password':   $('#pass1').val(),
        'level':      $('#userrole').val(),
        'property[]': properties
    };
    
    // submitting this way
    $.post(url, data)
        .done(function(response) {
            // continue
        })
        .fail(function(response) {
            // handle error
        });
    
    // or this way
    $.ajax({
        type: 'POST',
        url: url,
        data: JSON.stringify(data), // you'll have to change "property[]" to "property"
        contentType: "application/json",
        dataType: 'json',
        success: function(response) { 
            // continue
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效