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条)

报告相同问题?

悬赏问题

  • ¥15 Pyqt 如何正确的关掉Qthread,并且释放其中的锁?
  • ¥30 网站服务器通过node.js部署了一个项目!前端访问失败
  • ¥15 WPS访问权限不足怎么解决
  • ¥15 java幂等控制问题
  • ¥15 海湾GST-DJ-N500
  • ¥15 氧化掩蔽层与注入条件关系
  • ¥15 Django DRF 如何反序列化得到Python对象类型数据
  • ¥15 多数据源与Hystrix的冲突
  • ¥15 如何在线硕士了解,广告太多,希望有真实接触过的人回答下?(标签-学习|关键词-在线硕士)
  • ¥15 zabbix6.4与frp如何进行联动