2015-09-04 51 views
1

我正尝试使用angularjs将WCF RESTful C#API连接到phonegap应用程序。我的HTML在Web上完美工作,但在我的Windows手机中没有。AjaxCallBack没有被调用,windows phone phonegap

<!DOCTYPE html> 
 
<html xmlns="http://www.w3.org/1999/xhtml" data-ng-app="myApp"> 
 
<head> 
 
    <meta charset="utf-8" /> 
 
    <meta name="format-detection" content="telephone=no" /> 
 
    <meta name="msapplication-tap-highlight" content="no" /> 
 
    <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 --> 
 
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" /> 
 
    <link rel="stylesheet" type="text/css" href="css/index.css" /> 
 
    <title>Profile</title> 
 
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" /> 
 

 
</head> 
 
<body> 
 
    <div> 
 
     <div data-ng-controller="ProfileCtrl"> 
 
      First Name: {{FirstName}} 
 
      Last Name: {{LastName}} 
 
     </div> 
 
    </div> 
 

 
    <script type="text/javascript" src="cordova.js"></script> 
 
    <script type="text/javascript" src="js/index.js"></script> 
 
    <script type="text/javascript"> 
 
     app.initialize(); 
 
    </script> 
 
    <script type="text/javascript" src="js/jquery-1.11.3.min.js"></script> 
 
    <!--<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>--> 
 
    <script type="text/javascript" src="js/angular.min.js"></script> 
 

 
    <script> 
 

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

 
     window.jsoncallback = function (json) { 
 
      debugger; 
 

 
      if (!json.Error) { 
 

 
       alert("run callback"); 
 
       //alert("success"); 
 
      } 
 
      else { 
 

 
       alert(json.Message); 
 
      } 
 
     } 
 
     
 
     myApp.controller('ProfileCtrl', ["$scope", "$http", function ($scope, $http) { 
 

 
      function LoadProfile() { 
 

 
       debugger; 
 

 
       var call_url = 'http://localhost:53834/LoginService.svc/UserProfile/1'; 
 

 

 
       //For jsonp calling 
 
       //alert("Running Jsonp"); 
 
       //$http.jsonp(call_url).success(function (data) { 
 
       // debugger; 
 
       //}); 
 
       
 

 
       alert("Running Ajax"); 
 
       $.ajax({ 
 
        url: call_url, 
 
        type: "POST", 
 
        dataType: "jsonp", // from the server 
 
        crossDomain: true, 
 
        contentType: "application/json; charset=utf-8", // to the server 
 
        jsonpCallback: 'jsoncallback', 
 
        success: function (data) { 
 
         alert("success"); 
 
         $scope.FirstName = data.FirstName; 
 
         $scope.LastName = data.LastName; 
 
         $scope.$apply(); 
 
        }, 
 
       }).done(function (data) { 
 
        debugger; 
 
        console.log(data); 
 

 
       }).fail(function (xhr, status, error) { 
 
        debugger; 
 
        alert(xhr.status + " " + status + " " + error); 
 
       }); 
 
      }; 
 

 
      LoadProfile(); 
 
     }]); 
 

 
    </script> 
 

 
</body> 
 
</html>

这里是我的html代码,

服务代码

public Stream UserProfile(string profileID) 
    { 
     TestDBEntities db = new TestDBEntities(); 

     int _ProfileID = Convert.ToInt32(profileID); 
     tblProfile objtblProfile = db.tblProfiles.Find(_ProfileID); 

     string jsCode; 

     JavaScriptSerializer returnList = new JavaScriptSerializer(); 

     string output = returnList.Serialize(objtblProfile); 

     if (objtblProfile != null) 
      jsCode = "jsoncallback" + "(" + output + ");"; 
     else 
      jsCode = null; 

     WebOperationContext.Current.OutgoingResponse.ContentType = "application/javascript"; 
     return new MemoryStream(Encoding.UTF8.GetBytes(jsCode)); 


    } 

我已经做了所有的事情,但在我的Windows Phone不工作是给了一个错误

200 parserror: jsoncallback was not called

但它在Web上正常工作。

+0

你最好看看[this](https://docs.angularjs.org/api/ng/service/$http)而不是使用'$ .ajax'! –

+0

我有这个使用它也有同样的问题。 alert(“Running Jsonp”); $ http.jsonp(call_url).success(功能(数据){ 调试器; 警报( “成功”); $ = scope.FirstName data.FirstName; $ scope.LastName = data.LastName; $范围。apply(); })。error(function(data){ }); –

+0

它也没有调用成功的方法也在网络或windowsphone –

回答

0

我在我的应用程序中提出的AJAX请求的方式如下。看看是否适合你:

app.ajaxCall = function() { 
     var xhr = new XMLHttpRequest(); 
     xhr.open('POST', <URL>, false); 
     //Content-Type can be based on the content you are sending 
     xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
     var params = <define param string here>; 
     xhr.send(params); 

     var isError = false; 
     try { 
      if (xhr.status === 200) { 
       var jsonResponse = JSON.parse(xhr.responseText); 
       <logic> 
      } 
}; 

希望这会有所帮助。

+0

跨域问题即将到来,因为我的服务是在另一台服务器,这就是为什么我必须使用jsonp这段代码不适用于我。 –

+0

对于跨域请求,您可以在API中实现CORSFilter。 –

+0

如何做到这一点我在C#中使用WCF服务 –