简体   繁体   English

$ http仅在发出简单的ajax请求时调用错误?

[英]$http only calls error when making simple ajax request?

I feel like I'm missing something small. 我觉得我缺少一些小东西。 Here's my index.html: 这是我的index.html:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="ImageViewerApp">
<head>
    <title></title>
    <script type="text/javascript" src="/scripts/angular.js"></script>
    <script type="text/javascript" src="/scripts/Controller.js"></script>
</head>
<body ng-controller="ImageViewerCtrl">
    <p>{{Value}}</p>
</body>
</html>

My Controller.js: 我的Controller.js:

var ImageViewer = angular.module('ImageViewerApp', []);

ImageViewer.controller('ImageViewerCtrl', [ '$scope', '$http', ImageViewerCtrl ]);

function ImageViewerCtrl($scope, $http) {
    $scope.Value = 'Test';
    $http.get('data/job.json').success(function (data) {
        $scope.Value = data.Test;
    }).error(function (data, status, headers, config) {
        data = data;
    });
}

My job.json: 我的job.json:

{
  Test: 'Hello, World!'
}

And my Web.config: 而我的Web.config:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.webServer>
    <staticContent>
      <mimeMap fileExtension=".json" mimeType="application/json"/>
    </staticContent>
  </system.webServer>
</configuration>

I'm working in VS 2012 and I've made a simple blank website. 我在VS 2012中工作,我做了一个简单的空白网站。 Debugging the site runs it in IIS Express and IE 10. Other browsers might work, but the eventual project this will be used in will be an internal web app running on only IE 10. 调试站点可以在IIS Express和IE 10中运行它。其他浏览器可能也可以运行,但是最终将使用的项目是仅在IE 10上运行的内部Web应用程序。

I'm trying to do something similar to how the Angular Tutorial makes an AJAX request to a .json file in order to simulate a web request. 我正在尝试做与Angular Tutorial如何对.json文件发出AJAX请求以模拟Web请求的方式类似的操作。 I was first running into some issues about not having a handler defined for the .json file type, but I added a mimeMap to the Web.config and now fiddler reports that the request for /data/job.json is coming back as either a 200 or a 304 (Not Modified) but the error part of the promise is the only one that ever gets called. 我最初遇到一些有关没有为mimeMap文件类型定义处理程序的问题,但是我在Web.config中添加了mimeMap ,现在提琴手报告对/data/job.json的请求以两种方式返回200或304(未修改),但是promise的错误部分是唯一被调用过的部分。

data = data; is there simply so I can place a breakpoint in Visual Studio. 只是在那里,所以我可以在Visual Studio中放置一个断点。 data , status , and headers are all undefined and config doesn't seem to say anything useful, but here's the result of JSON.stringify(config) if it helps: datastatusheaders都是未定义的,并且config似乎没有任何用处,但是如果有帮助,这是JSON.stringify(config)的结果:

{"method":"GET","transformRequest":[null],"transformResponse":[null],"url":"/data/job.json","headers":{"Accept":"application/json, text/plain, */*"}}

What am I doing wrong that sets off error instead of calling success ? 我在做错什么而不是打电话给success引发了error

EDIT: Wandering through the call stack and looking at the state inside angular.js, I've found that somewhere in the bowels of angular it does have the response I expect for requesting job.json, but later in the stack there's an error (that isn't successfully passed to my error callback) that simply says Invalid Character . 编辑:徘徊在调用堆栈中,并查看angular.js内的状态,我发现在angular肠道的某个地方确实有我期望的请求job.json的响应,但是稍后在堆栈中有一个错误(不能成功传递给我的错误回调),只是说Invalid Character I don't know what character it could be hanging up on. 我不知道它会挂什么角色。 This hasn't helped me figure out what's going wrong, but maybe it'll help get an answer here. 这并没有帮助我找出问题所在,但是也许可以在这里找到答案。

{Test: "value"}

It isn't valid JSON when returning it in it's RAW form. 以RAW形式返回时,它不是有效的JSON。 You will need to add the appropriate " (Quotes) around the key . 您将需要在key周围添加适当的“(引号)”。

{"Test": "value"}

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

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