最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Web page can't retrieve data from remote server in Phonegap (but works fine in Chrome) - Stack Overflow

programmeradmin1浏览0评论

I am trying to create an Android application using Phonegap 3.1. The app needs to retrieve data from my remote server, which is running Apache with the Header set Access-Control-Allow-Origin "*" directive set for the URI the app should be pulling data from. I have <access subdomains='true' url='*' /> set in Phonegap's config.xml file. The code below works and runs the "success" function if I put the web page into a local Apache directory separate from the server (different machines on different domains in different physical locations) and call it from Chrome, but when I try to call it via Phonegap it throws a less than helpful error of {"readystste":0,"responseText":"","status":0,"statusText":"error"}. The "readystate":0 looks like it might be an access or cross-site scripting error that's only happening in Phonegap, so I'm willing to believe it's something I need to configure on my end, but I'm going nuts trying to figure out what.

Thanks for any help you can provide. I need to get this running and have been scouring the web for a week trying to get it to work.

<!DOCTYPE HTML>
<HTML>
<HEAD>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <link rel="stylesheet" type="text/css" href="css/affirmation.css">
  <script src="js/jquery-1.10.1.min.js"></script>
  <script>

  $.support.cors = true;

  $(document).ready(function(){

      $.ajax({
        type: 'POST',
        url: '/info/API',
        datatype: 'json',
        data: JSON.stringify(
          { method: 'content',
          params: [
            'text passed to function on server' 
          ],
          id: '1' }
        ),
        success: function(response, textstatus, jqxhr) {
        $('#graf1').text(JSON.stringify(response));
        $('#graf3').text(JSON.stringify(textstatus));
        $('#graf6').text(JSON.stringify(jqxhr));
        },
        error: function(jqxhr, textstatus, errorthrown) {
        $('#graf1').text(JSON.stringify(jqxhr));
        $('#graf3').text(JSON.stringify(textstatus));
        $('#graf6').text(JSON.stringify(errorthrown));
        }

      });          
  });

  </script>

</HEAD>
<BODY>
  <DIV ID="header">JSON Server Test</DIV>
  <DIV ID="content">
  <DIV ID="graf1" CLASS="bodytext"></DIV>
  <DIV ID="graf3" CLASS="bodytext"></DIV>
  <DIV ID="graf6" CLASS="bodytext"></DIV>
</BODY>
</HTML>

I am trying to create an Android application using Phonegap 3.1. The app needs to retrieve data from my remote server, which is running Apache with the Header set Access-Control-Allow-Origin "*" directive set for the URI the app should be pulling data from. I have <access subdomains='true' url='*' /> set in Phonegap's config.xml file. The code below works and runs the "success" function if I put the web page into a local Apache directory separate from the server (different machines on different domains in different physical locations) and call it from Chrome, but when I try to call it via Phonegap it throws a less than helpful error of {"readystste":0,"responseText":"","status":0,"statusText":"error"}. The "readystate":0 looks like it might be an access or cross-site scripting error that's only happening in Phonegap, so I'm willing to believe it's something I need to configure on my end, but I'm going nuts trying to figure out what.

Thanks for any help you can provide. I need to get this running and have been scouring the web for a week trying to get it to work.

<!DOCTYPE HTML>
<HTML>
<HEAD>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <link rel="stylesheet" type="text/css" href="css/affirmation.css">
  <script src="js/jquery-1.10.1.min.js"></script>
  <script>

  $.support.cors = true;

  $(document).ready(function(){

      $.ajax({
        type: 'POST',
        url: 'http://www.remote.server/info/API',
        datatype: 'json',
        data: JSON.stringify(
          { method: 'content',
          params: [
            'text passed to function on server' 
          ],
          id: '1' }
        ),
        success: function(response, textstatus, jqxhr) {
        $('#graf1').text(JSON.stringify(response));
        $('#graf3').text(JSON.stringify(textstatus));
        $('#graf6').text(JSON.stringify(jqxhr));
        },
        error: function(jqxhr, textstatus, errorthrown) {
        $('#graf1').text(JSON.stringify(jqxhr));
        $('#graf3').text(JSON.stringify(textstatus));
        $('#graf6').text(JSON.stringify(errorthrown));
        }

      });          
  });

  </script>

</HEAD>
<BODY>
  <DIV ID="header">JSON Server Test</DIV>
  <DIV ID="content">
  <DIV ID="graf1" CLASS="bodytext"></DIV>
  <DIV ID="graf3" CLASS="bodytext"></DIV>
  <DIV ID="graf6" CLASS="bodytext"></DIV>
</BODY>
</HTML>
Share Improve this question asked Oct 14, 2013 at 18:39 CreedeCreede 1531 silver badge12 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

Try putting <access origin="*" subdomains="true" /> inside the www/config.xml file, as per: http://cordova.apache/docs/en/3.1.0/guide_appdev_whitelist_index.md.html#Whitelist%20Guide In your question, you have url instead of origin.

Another thing I'm thinking of, are you testing with an emulator? If so, are you using the full URL for the remote server or just localhost? Because localhost on the emulator will map back to the emulator itself and not to the server you are running on your machine.

Did you have the permissions for internet use? PhoneGap Documentation: Connection

I tried everything including <access origin="*" subdomains="true" /> but din't work for me.

then I added

<plugin name="cordova-plugin-whitelist" source="npm" spec="~1.2.1" />

In my config.xml and it worked.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论