简体   繁体   English

使用Ajax将JSON发送到PHP,数据麻烦

[英]Sending JSON to PHP using ajax, troubles with data

my javascript won't go into my Database.php file. 我的JavaScript不会进入我的Database.php文件。 Anyone knows what's wrong? 有人知道怎么了吗?

I know there is another thread with this question but it just doesn't work for me. 我知道这个问题还有另一个话题,但这对我不起作用。

I have this in javascript 我在javascript中有这个

   var Score = 5;

//Score insert
        var postData = 
    {
        "Score":Score
    }
                $.ajax({
                          type: "POST",
                          dataType: "json",
                          url: "Database.php",
                          data: {myData:postData},                    
                          success: function(data){
                                alert('Items added');
                          },
                          error: function(e){
                                console.log(e.message);
                          }
                });         

and this in php 而这在PHP

   function InsertScore(){  
    $table = "topscores";

    if(isset($_POST['myData'])){
     $obj = json_encode($_POST['myData']);

     $stmt = $conn->prepare("INSERT INTO " + $table + " VALUES (?)");
     $stmt->bind_param('s', $obj);
     $stmt->execute();
    }
    else{
        console.log("neen");
        }

    $result->close();

change this line 改变这条线

success: function InsertScore(data){

to this 对此

success: function(data){

the success parameter of jquerys ajax method has to be a anonymous function (without a name) or one defined in javascript but definitely not a php function. jquery ajax方法的成功参数必须是匿名函数(无名称)或用javascript定义的函数,但绝对不是php函数。

You should read up on variable scope , your $table variable is not defined in the scope of your function. 您应该阅读变量范围 ,您的$table变量未在函数范围内定义。

You also have an sql injection problem and should switch to prepared statements with bound variables. 您也有一个SQL注入问题,应该切换到带有绑定变量的准备好的语句。

You are trying to send an object to your PHP file instead of a JSON data type. 您正在尝试将对象而不是JSON数据类型发送到您的PHP文件。 Try 2 use JSON2 to stringify your object like this : 尝试2使用JSON2像这样对您的对象进行字符串化:

var scoreINT = 9000;
var usernameSTRING = "testJSON"

var scoreOBJ = {score:scoreINT,username:usernameSTRING};
var jsonData = JSON.stringify(scoreOBJ);

this would give you the following result "{"score":9000,"username":"testJSON"}" 这将为您提供以下结果“ {” score“:9000,” username“:” testJSON“}”

You would be able to send this with your AJAX if you change ( if you follow my variable names ofcourse ) 如果您进行更改(如果您遵循我的课程的可变名称),则可以将其与AJAX一起发送

data: {myData:postData}

to

data: {myData:jsonData}

This would already succesfully transfer your data to your PHP file. 这样就可以成功将数据传输到PHP文件中。

regarding your error messages and undefined. 有关您的错误消息和未定义。 the message "e.message" does not exist. 消息“ e.message”不存在。 so thats the "undefined" you are getting. 这样多数民众赞成在“未定义”的。 no worries here. 这里不用担心。

I noticed the succes and error are called incorrectly. 我注意到成功和错误被错误地调用。 I've just deleted them because there is no need to. 我刚刚删除了它们,因为没有必要。

Next. 下一个。 moving up to your PHP. 升级到您的PHP。

you would rather like to "DECODE" then to encode your encoded JSON. 您希望先进行“解码”,然后对编码的JSON进行编码。 you could use the following there : 您可以在其中使用以下内容:

$score = json_decode($_POST['json'],true);

the extra param true is so you are getting your data into an array ( link ) or you could leave the true so you are working with an object like you already are. 额外的参数true是,因此您可以将数据放入数组( link )中,也可以保留true,这样就可以像处理现有对象一样使用对象。

Greetings ySomic 问候ySomic

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

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