如何获取名称为''的数组内容(空字符串)

时间:2016-07-23 17:35:42

标签: typescript

以下是我获取值的地方:

registerUser(localUser: LocalUser) {
    this.registrationService.registerUser(this.localUser)
        .subscribe(
            resp => this.statusMessage = resp,
            error => {
                if(error._body != undefined) {
                    let arr = error._body.modelState;
                    var errorObject  = JSON.parse(error._body);
                    }
            },
            () => this.completeRegister());
}

以下是我从http请求返回的错误对象,使用asp.net和webapi 2注册新客户端。如您所见,模型状态是包含数组的对象。

这是从webapi返回的json,我将JSON.parse返回到errorObject:

    "{   
         "message": "The request is invalid.",   
         "modelState": {
                 "": [
                        "Name jbaird@test.com is already taken.",
                        "Email 'jbaird@test.com' is already taken."
                     ]
         } 
      }"

enter image description here

我今天已经尝试过所有我能想到的东西,使用打字稿来获取那些数组值,一切都是徒劳的。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

由于数组是名为空字符串的属性的值,您可以像这样访问它

$server   = "localhost";
$username = "root";
$password = "root3";
$dbname   = "sampledb";

// creat connection
$connection = new mysqli($server, $username, $password, $dbname);

// check the connection
if ($connection->connect_error) {
    die("connect to database failed :" . $connection->connect_error);
}
 // check value from select menu 
if ($_POST['typelist'] == 'item') {
    // prepare and bind parameters
    if (isset($_FILES['item_image'])) {
        $itemquery = $connection->prepare("insert into items(item_image,  item_manufacturing_year, item_Length, item_width, item_weight, item_price, item_description, item_model) VALUES (? , ? , ? , ? , ? , ? , ? , ?)");
        $itemquery->bind_param("bsssssss", $image, $manufacturing_year, $length, $width, $weight, $price, $description, $model);

        // set all parameters and execute the query
        $image = $_FILES['item_image']['name'];
        $temp  = $_FILES['item_image']['tmp_name'];
        $path  = "../images/items/$image";
        move_uploaded_file($temp, $path);
        $manufacturing_year = $_POST['manufacturing_year'];
        $length      = $_POST['item_length'];
        $width       = $_POST['item_width'];
        $weight      = $_POST['item_weight'];
        $price       = $_POST['item_price'];
        $description = $_POST['item_description'];
        $model       = $_POST['item_model'];
        $itemquery->execute();
        $itemquery->close();
        $connection->close();
    } 
}

enter image description here