Can't get json data from jQuery ajax call

后端 未结 5 1087
深忆病人
深忆病人 2021-01-05 09:35

I\'m trying to get data from data.php via jQuery ajax call.

My code looks like this:

var jsonData;

$.ajax({
        url: \'data.php\',
         


        
5条回答
  •  -上瘾入骨i
    2021-01-05 09:52

    PHP

    try {
        $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);  
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $dbh->query('SET NAMES utf8;');
        $stmt = $dbh->prepare($sql);  
        //$stmt->bindParam("id", $_GET[id]);
        $stmt->execute();
    
        $advice = $stmt->fetchAll(PDO::FETCH_OBJ);
        $dbh = null;
        echo '{"items":'. json_encode($advice) .'}'; 
    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}'; 
    }
    

    Ajax

     var temp;
        $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: serviceurl,
                data: "{'userName':'" + userName + "' , 'password': '" + password                                   
                       + "'}",
                dataType: "json",
                success: function(msg) {
                                temp = jQuery.parseJSON(msg.d);
                              },
                error: function(xhr, ajaxOptions, thrownError) {}
    
            });
    

提交回复
热议问题