简体   繁体   English

JSON,jquery和ajax解析问题

[英]JSON, jquery, and ajax parsing issue

Here's my java script file: 这是我的Java脚本文件:

$('#addSchoolForm').trigger("reset");

//$(document).ready(function()  {
$(function() {
    $("#dialog").dialog({
        autoOpen: false,
        maxWidth:600,
        maxHeight: 350,
        width: 500,
        height: 300,
    });

    $("#addSchool").on("click", function() {
        $("#dialog").dialog("open");
    });

    $("#addSchoolForm").submit(function(e) { 
        e.preventDefault();
        $("#dialog").dialog("close")
        var postData = jQuery(this).serialize();
        $.ajax({
            type: "POST",
            url: "AddSchools.php",
            data: postData,
            success: function(data){
                alert(data); }
        });   
    });

    $("#editSchool").submit(function(e) { 
        e.preventDefault();
        var editData = jQuery(this).serialize();
        $.ajax({
            type: "POST",
            url: "GetSchoolID.php",
            data: editData,
            dataType: 'json',
            success: function(data){


                var schoolID = $.parseJSON(data);
                alert("success");
                alert(schoolID.name);

                //alert(data["json"]);
                //alert(data); 

                //document.addSchoolForm[sname].value = data[0].name;
                //document.addSchoolForm[abbrev].value = data[abbrev];
                //document.addSchoolForm[abbrev].value = data[0].abbrev;
            }

            alert(schoolID.name); 
        });
        //$("#dialog").dialog("open");
    });
}) 

And here's my get schoolID php file 这是我得到的schoolID php文件

<?php
$school_id = $_POST['school_id'];


$db = mysqli_connect("localhost", "root", "imagroup123","mytrack");

if(!$db){
    exit("Error in database connection");
    echo("couldn't connect to database");
}
else{
    $q = "SELECT * FROM `School` WHERE `SchoolID`='$school_id'";
    $schoolresults = mysqli_query($db,$q);

    $row = mysqli_fetch_assoc($schoolresults);
    $school["name"] = $row['SchoolLong'];
    $school["abbrev"] = $row['SchoolShort'];

    echo json_encode($school);

    }


?>

When I just tested the php file with jsonlint.com I get a correct json object but it's not getting carried through the javascript file. 当我刚刚使用jsonlint.com测试php文件时,我得到了一个正确的json对象,但它并没有通过javascript文件传输。 I'm fairly new to this so I'm pretty suck with this problem. 我对此很陌生,所以我很讨厌这个问题。 I also want to add the data to a form values and then open the dialog form after. 我还想将数据添加到表单值中,然后再打开对话框表单。

Change: 更改:

        success: function(data){
            var schoolID = $.parseJSON(data);

to: 至:

        success: function(schoolID){

because $.ajax automatically calls $.parseJSON() when you specify dataType: 'json' . 因为$.ajax在您指定dataType: 'json'时自动调用$.parseJSON()

Here is the php backend structure that I use all the time: 这是我一直使用的php后端结构:

$query = " SELECT * 
           FROM school 
           WHERE SchoolID = $school_id;
$result = mysqli_query($cxn, $query) or die ("could not query database 1");

if (mysqli_num_rows($result) > 0)
{
    $row = mysqli_fetch_array($result);
    $variablestopass = array
    (
           'schoolname' => $row['SchoolLong'],
         'schoolabbrev' => $row['SchoolShort'],
     );
  echo json_encode($variablestopass);}
else
  { echo "Error selection id"; } 

And here is some js to call it and read it: 这是一些调用它并阅读它的js:

$.ajax({
        type: 'POST',
         url: 'thenameofyourfile.php';
        data: {schoolid: schoolid},
        dataType: 'json'
        })
         .done( function() { alert( "Got it!"" );
                             Do other stuff here
                            })
         .fail(function(jqXHR, textStatus, errorThrown){
          console.log(jqXHR.responseText, textStatus, errorThrown);
    });

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

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