Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ASP MVC基本AJAX Json请求返回null_Javascript_Jquery_Ajax_Asp.net Mvc_Angularjs - Fatal编程技术网

Javascript ASP MVC基本AJAX Json请求返回null

Javascript ASP MVC基本AJAX Json请求返回null,javascript,jquery,ajax,asp.net-mvc,angularjs,Javascript,Jquery,Ajax,Asp.net Mvc,Angularjs,我有一个MVC应用程序,其中有一个名为AngularJS的控制器(我也使用AngularJS),它有一个名为GetQuestion的操作。该操作返回一个JsonResult,如下所示(摘自Chrome): 我的JS函数如下: var request = $.ajax({ url: "/Angular/GetQuestion", dataType: "json", type: "post", success

我有一个MVC应用程序,其中有一个名为AngularJS的控制器(我也使用AngularJS),它有一个名为GetQuestion的操作。该操作返回一个JsonResult,如下所示(摘自Chrome):

我的JS函数如下:

var request = $.ajax({
    url: "/Angular/GetQuestion",
    dataType: "json",
    type: "post",
    success: (function (data) { alert(data); })
});
function QuestionCtrl($scope, $http) {

$http.post('/Angular/GetQuestion',null).success(function(data) {
    $scope.answers = data.Answers;
    $scope.imgPath = data.game.ImgPaths[0];
    //console.log($scope.answers);
    //console.log($scope.imgPath);
});
但警报窗口只显示[object],而不是我上面写的Json

更新 好的,这已经解决了,塔克斯。然而,正如您可能怀疑的,我的目标不是在警报框中显示此数据,而是以某种方式使用它。这是我的控制器

函数($scope){

然后是视图:

<div ng-controller="QuestionCtrl">

<img class="quizImage" src="~/Gallery/{{imgPath}}"/>
@using (Html.BeginForm("Answer", "Angular", FormMethod.Post))
{
    <p ng-repeat="answer in answers"><input type="radio" name="game"  value="{{answer}}"/> {{answer}}</p>
    <p><input type="submit" value="Answer"/></p>
}
</div>

@使用(Html.BeginForm(“Answer”,“Angular”,FormMethod.Post))
{

{{answer}

}

我既没有图像也没有问题。如果我在controller中硬编码它们,那么就可以了。

一个警报将显示这一点,我建议使用console.log(数据)

或如评论所述:

var request = $.ajax({
url: "/Angular/GetQuestion",
dataType: "json",
type: "post",
success: (function (data) { alert(JSON.stringify(data)); })
 });

警报将显示,我建议使用console.log(数据)

或如评论所述:

var request = $.ajax({
url: "/Angular/GetQuestion",
dataType: "json",
type: "post",
success: (function (data) { alert(JSON.stringify(data)); })
 });

之所以会发生这种情况,是因为JSON是JavaScript中的一个对象

alert(data);
它将尝试将对象强制转换为字符串,在本例中,该字符串只输出它是对象的事实

要查看对象的内容,可以编写一个简单的函数,与alert或console.log一起使用

function outputProperties(anObject) {
    var props = '';
    for (var prop in anObject) {
        props += '\n' + prop + ' value: ' + anObject[prop];
    }
    return props;
}
像这样使用它

alert(outputProperties(data));

之所以会发生这种情况,是因为JSON是JavaScript中的一个对象

alert(data);
它将尝试将对象强制转换为字符串,在本例中,该字符串只输出它是对象的事实

要查看对象的内容,可以编写一个简单的函数,与alert或console.log一起使用

function outputProperties(anObject) {
    var props = '';
    for (var prop in anObject) {
        props += '\n' + prop + ' value: ' + anObject[prop];
    }
    return props;
}
像这样使用它

alert(outputProperties(data));

我这样解决了我的第二个问题:

var request = $.ajax({
    url: "/Angular/GetQuestion",
    dataType: "json",
    type: "post",
    success: (function (data) { alert(data); })
});
function QuestionCtrl($scope, $http) {

$http.post('/Angular/GetQuestion',null).success(function(data) {
    $scope.answers = data.Answers;
    $scope.imgPath = data.game.ImgPaths[0];
    //console.log($scope.answers);
    //console.log($scope.imgPath);
});
}


请注意,这是AngularJS。

我像这样解决了第二个问题:

var request = $.ajax({
    url: "/Angular/GetQuestion",
    dataType: "json",
    type: "post",
    success: (function (data) { alert(data); })
});
function QuestionCtrl($scope, $http) {

$http.post('/Angular/GetQuestion',null).success(function(data) {
    $scope.answers = data.Answers;
    $scope.imgPath = data.game.ImgPaths[0];
    //console.log($scope.answers);
    //console.log($scope.imgPath);
});
}


请注意,首先是AngularJS。

。。。当您为图像动态构建src url时(使用Angular中的{{expression}}语法),您不需要使用“src”属性,而需要使用“ng src”Angular指令。它允许在加载图像之前有一定的时间处理url

首先。。。当您为图像动态构建src url时(使用Angular中的{{expression}}语法),您不需要使用“src”属性,而需要使用“ng src”Angular指令。它允许在加载图像之前有一定的时间处理url

或者,如果没有控制台可用,或者出于某种原因他只想使用alert,他可以始终执行
alert(JSON.stringify(data))。但是,现在哪个浏览器没有控制台?或者,如果没有控制台可用,或者出于某种原因他只想使用alert,他可以始终执行
alert(JSON.stringify(data))。但是,现在哪个浏览器没有控制台?请参阅我的答案以了解解决图像源问题的方法。请参阅我的答案以了解解决图像源问题的方法。