3

你好我有一个角JS控制器这样制造AJAX调用在角JS填充范围数据

function InstantSearchController($scope){ 

$scope.items = [ 
    { 
     url: 'http://tutorialzine.com/2013/04/services-chooser-backbone-js/', 
     title: 'Your First Backbone.js App – Service Chooser', 
     image: 'http://cdn.tutorialzine.com/wp-content/uploads/2013/04/service_chooser_form-100x100.jpg' 
    } 
]; 

}

和我有一个AJAX调用

function getListOfJsonObjects(){ 
$.ajax({ 
    url:"http://"+window.location.host+"/getListOfJsonObjects", 
    type: "GET", 
    beforeSend: function (request) 
    { 
     request.setRequestHeader("JSESSIONID", $.cookie("JSESSIONID")); 
    }, 
    dataType: 'jsonp', 
    data: { 
     foreignKeyType:"OrgChartJSon", 
    }, 
    success: 
     function(dataFromServer){ 
     var parsedJSON = jQuery.parseJSON(JSON.stringify(dataFromServer));     

    }, 
    error: 
     function(xhr, status, error){ 

     alert("Failed"); 
    } 
}); 

}

我怎样才能使一个$ http.jsonp调用,以便将响应数据放入作用域项目中。 请帮我试用var responsePromise = $http.jsonp(url, {params : {foreignKeyType:"DecisionTreeJSon"} }); 甚至认为响应状态是200它始终进入失败方法。

角码:

<div ng-app="instantSearch" ng-controller="InstantSearchController"> 

<div class="bar"> 
    <!-- Create a binding between the searchString model and the text field --> 
    <input type="text" ng-model="searchString" placeholder="Enter your search terms" /> 
</div> 

<ul> 
    <!-- Render a li element for every entry in the items array. Notice 
     the custom search filter "searchFor". It takes the value of the 
     searchString model as an argument. 
    --> 
    <li ng-repeat="i in items | searchFor:searchString"> 
     <a href="{{i.url}}"><img ng-src="{{i.image}}" /></a> 
     <p>{{i.title}}</p> 
    </li> 
</ul> 

+1

如果您发布了完整的Angular代码而不是jQuery,将会很有帮助。 – MBielski

回答

2

不要在AngularJS项目在所有使用jQuery。

您必须使用$ http $ resource services来查询Web服务。

Here is a plunker以显示如何使用$ http来填充范围,它不是jsonp,但您应该轻松扩展此示例。

+0

如果我使用$ http我得到XMLHttpRequest无法加载http:// getListOfJsonObjects /?foreignKeyType = JSon。请求的资源上没有“Access-Control-Allow-Origin”标题。错误 – Ekata