【问题标题】:Java-AngularJS http get method gets the string with "" and escape charactersJava-AngularJS http get 方法获取带有“”和转义字符的字符串
【发布时间】:2017-12-14 00:53:35
【问题描述】:

我用这个函数发出一个http get请求:

    $scope.fetchResult = function() {
    $http.get('search/result').success(function(getResult){
        $scope.result = getResult;
    });
};

这是后端函数:

    @RequestMapping("/result")
public @ResponseBody String getResult() {
    return searchService.getResult();
}

问题是它将字符串打印到网页,开头为“字符,结尾也转义字符。我真的找不到这背后的任何逻辑。例如,如果字符串是:

String result = "Hello";

它不会在网页上打印 Hello,而是打印“Hello”。另一个例子:

String result = "Found \"" + keyword + "\" '" + count + "' times at \"" + website + "\"";

它也打印转义字符。所以假设关键字是关键字,计数是 0,网站是http://stackoverflow.com 结果是这样打印在网页上的:

"Found \""keyword"\" '"0"' times at \""http://stackoverflow.com"\""

虽然我显然期望:

Found "keyword" '0' times at "http://stackoverflow.com"

我该如何解决这个问题?这对我来说真的没有任何意义我是 js 和 Web 开发的新手。

编辑:如果我使用 List<String> 而不是 String 并在 html 表单上执行此操作:

<p ng-repeat:"res in result">{{res}}<p>

它有效。但我真的不想这样使用它,因为我不需要列表,只需要一个字符串。

【问题讨论】:

  • 你是指Javascript还是Java?
  • 我的意思是java。我在后端使用 java,在前端使用 angularjs。
  • 你是怎么打印的?
  • 我猜您正在使用 Spring 搜索 RequestMapping,如果是这种情况,您可能需要更改选项以输出您想要的格式。我建议使用 JSON 响应而不是裸字符串,或者创建一个 AngularJS $http 拦截器来在到达回调之前转换数据。仅供参考,successerror 现在已完全弃用并从 1.6 开始删除。
  • @Phix 也尝试返回 JsonObject,但它也没有奏效。 Prashant 我使用 {{result}} 在 html 页面上打印它。这就是 AngularJS 的工作原理,它与问题无关。

标签: javascript java angularjs json spring-mvc


【解决方案1】:

Spring 有一个叫做 Suffix Strategy 的东西,它使框架能够直接从 URL 检查路径扩展以确定输出内容类型。

您看到的编码结果是因为@RequestMapping 被设置为/result.json 并带有.json 扩展名,所以结果作为Json 对象返回。

要解决此问题,您有多种选择。

选项 1:

在您的 Javascript 中将返回的响应作为 Json 对象处理。

选项 2:

@RequestMapping 值重命名为 /result。下面是一个代码示例:

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {
   @CrossOrigin(origins = "*")
   @RequestMapping(value="/result")
   public @ResponseBody String sayHello() {
      String website = "http://stackoverflow.com";
      int count = 4;
      String keyword = "keyword";
      return "Found \"" + keyword + "\" '" + count + "' times at \"" + website + "\"";
   }
}

Javascript(此处添加接受标头是可选的)

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
  $http.get("/result",{headers: { 'Accept': 'text/plain' }})
  .then(function(response) {
      $scope.result = response.data;
  });
});
</script>

选项 3

@RequestMapping 值保留为/result.json,但使用WebMvcConfigurerAdapter 禁用后缀策略。下面是一个代码示例

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {
   @CrossOrigin(origins = "*")
   @RequestMapping(value="/result.json", method = RequestMethod.GET, produces="text/plain")
   public @ResponseBody String sayHello() {
      String website = "http://stackoverflow.com";
      int count = 4;
      String keyword = "keyword";
      return "Found \"" + keyword + "\" '" + count + "' times at \"" + website + "\"";
   }
}

WebMvcConfigurerAdapter 类

import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false)
        .favorParameter(false)
        .ignoreAcceptHeader(false)
        .useJaf(false)
        .defaultContentType(MediaType.TEXT_PLAIN);
    }
}

Javascript

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
  $http.get("/result.json",{headers: { 'Accept': 'text/plain' }})
  .then(function(response) {
      $scope.result = response.data;
  });
});
</script>

【讨论】:

  • 如果内容类型设置为application/json,angularjs的$http.get不会自动将内容解析为JSON吗?
  • 当我按照你说的那样使用它时,它什么也得不到。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-13
  • 2015-10-16
相关资源
最近更新 更多