使用angular js ajax调用调用php json服务文件

时间:2014-09-09 10:36:48

标签: javascript angularjs

正在处理角度js,我需要调用一个以json格式返回数据的php服务文件,如果我将数据(json)放在本地工作正常,我无法从角度js做出ajax请求

var test = angular.module('TestService', []);

test.factory('TestService', function($http){

            return{

                  getTheTest:function(callback){


             $.ajax({
                    type: "GET",
                   // url: "https://angularjs.org/greet.php?callback=JSON_CALLBACK&name=Super%20Hero",
                    url: 'http://www.domain.com/alumni/wsQuestions.php?courseId=1&modeId=1',
                    contentType: "application/json",
                    dataType: "jsonp",
                    processData: true,
                    success: function (data, status, jqXHR) {
                    alert('Success :'+JSON.stringify(data));
                    console.log('Success :' + JSON.stringify(jqXHR) +"\n"+ "response: "+ JSON.stringify(data));
                    },
                    error: function (xhr) {
                  alert('Error :' +xhr.responseText);
                    console.log('error'+ JSON.stringify(xhr));
                    }
                    });



             }


             }
});

1 个答案:

答案 0 :(得分:0)

我建议您使用内置的AngularJS $ http服务。官方文档,带有示例:

https://docs.angularjs.org/api/ngResource/service/ $ HTTP

请记住将$ http注入您的服务或控制器。