Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.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
Angularjs POST返回415(不支持的媒体类型)_Angularjs_Rest - Fatal编程技术网

Angularjs POST返回415(不支持的媒体类型)

Angularjs POST返回415(不支持的媒体类型),angularjs,rest,Angularjs,Rest,我相信我在资源和http请求上都设置了正确的数据类型,但由于某些原因,我仍然没有使用REST方法,而是返回415 errormessage 休息方法: @POST @XmlElement(name = "query") @Path("/query") @Consumes({ "text/turtle" }) @Produces({ "text/turtle" }) public Response query(Query query) {...} 角度HTTP请求 $scope.searchCo

我相信我在资源和http请求上都设置了正确的数据类型,但由于某些原因,我仍然没有使用REST方法,而是返回415 errormessage

休息方法:

@POST
@XmlElement(name = "query")
@Path("/query")
@Consumes({ "text/turtle" })
@Produces({ "text/turtle" })
public Response query(Query query) {...}
角度HTTP请求

$scope.searchContact = function (q) {
        var uri = $scope.path;
        $http({
          method: 'POST', 
          url: uri,
          data: q,
          headers: {
            //'Content-Type': 'text/turtle',
            'Accept': 'text/turtle'
          },
          withCredentials: true
        }).
        success(function(data, status, headers) {
          if (status == 200 || status == 201) {
              $scope.queryResult = data;
              //$("#query").val(data);
              $scope.searchcontact = {};
          }
        }).
        error(function(data, status) {
          if (status == 401) {
            notify('Forbidden', 'Authentication required to create new resource.');
          } else if (status == 403) {
            notify('Forbidden', 'You are not allowed to create new resource.');
          } else {
            notify('Failed '+ status + data);
          }
        });
    };
感谢您的帮助。提前谢谢

已解决:


我正在发送字符串,但需要查询对象。所以一旦把它改成了字符串,一切都成功了

REST方法似乎只接受文本/海龟输入作为请求主体。由于您注释掉了angular请求的content-type头,它可能默认为text/plain或其他依赖于您的数据的内容。然后415将是您的服务器,通知您它不接受来自该类型的请求主体。您是否尝试过取消该行的注释,或者将@Consumes指令设置为请求正文的实际内容类型?@Fasermaler,是的,我尝试过取消注释。它给出了相同的错误。奇怪的是,您是否尝试过使用浏览器工具(F12)检查网络请求以查看它实际为请求指定的内容类型?是的,当然,请求和响应标题中的内容类型都是相同的。