Phonegap Ajax和wampserver / php

时间:2016-09-20 23:11:46

标签: php ajax cordova wamp

我目前正在尝试简单地让Ajax'激活'wampserver上的php文件 最好显示我认为解释的代码 以下是一个按钮激活的功能,HTML / JS一切正常

var jsonString = "";
function jsonconversion(){

    jsonString = JSON.stringify(myArr);
    return jsonString;
    console.log(jsonString);

    $.ajax({        
       type: "POST",
       url: "http://xx.xx.xx.xx:80/xx/index2.php",
       data: jsonString,
    complete: function() {
            console.log("Success");
    },
    error: function() {
           console.log("Function: forward_to_server() error")
    }
    }); 
}

但是,我似乎无法访问php文件。为了方便起见,我甚至没有在php文件中使用jsonString,而只是尝试将一些整数发布到wamp上的数据库中。加载的php文件本身可以正常工作

<?php

$servername = "localhost";
$username = "root";
$password = "xx";
$dbname = "xx";

//$myArray = $_REQUEST['jsonString'];
$myArray = array(100,1,19);

$con = mysqli_connect($servername,$username,$password ,$dbname) or die ("could not connect database");
echo $myArray;

$keys = array_keys($myArray);              // get the value of keys
$rows = array();                         // create a temporary storage for rows
foreach($keys as $key) {                 // loop through
    $value = $myArray[$key];               // get corresponding value
    $rows[] = "('" . $key . "', '" . $value . "')";
                                         // add a row to the temporary storage 
}
$values = implode(",", $rows);           // 'glue' your rows into a query
$sql = "INSERT INTO php_test (temp, sound, light) VALUES (10,20,30)"; //. $values
$rs = mysqli_query($con,$sql) or die(mysqli_error($con));

if ($con->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $con->error;
}

$con->close();

?>

然而,当我在phonegap中调用该函数时,它似乎不起作用,我没有任何错误,这会伤害过程:(

Wamp服务器是“在线”,我可以从互联网上访问它。

对此有何指导?

提前致谢

enter image description here

0 个答案:

没有答案