Ajax json从域获取数据

时间:2022-10-18 09:37:28

I am working on phonegap and trying to get list of user using ajax call but my function is returning nothing on mobile its working fine in browser

我正在使用phonegap,并试图通过ajax调用获取用户列表,但我的功能是在浏览器中不返回任何移动的功能。

here is the function and url data is in xml format

这是函数,url数据是xml格式

$(document).ready(function(){
$.ajax({
    type: "POST",
    url: "http://my_server_url/user/selectuser",
    dataType: 'jsonp',
    crossDomain: true,
    processData: true,
    cache: false,
    success: function(data)
    {
        alert("Success:"+data.detail[0].Username);
    },
    error: function(data){
        alert("Error:"+data);
    },
    statusCode: {
        404: function() {
            alert( "page not found" );
        }
    }
}); 
});

Thanks

谢谢

2 个解决方案

#1


1  

The general steps I go through for checking problems like this on mobile are:

我在手机上检查这类问题的一般步骤是:

  1. The simplest thing you can do is try pulling up the resource via a regular browser window on your phone and see what's different. Just type in the URL and see what happens. If you can't pull it down on a generic Chrome / Mobile Safari tab, the error may help you figure out what the problem is. If you can pull it down, the problem likely resides in your code (a typo in the URL perhaps). This won't work if you aren't doing a GET request, but it may at least help you figure out if your phone can reach your server.
  2. 你能做的最简单的事情就是通过你手机上的一个普通浏览器窗口打开资源,看看有什么不同。只需输入URL,看看会发生什么。如果你不能在普通的Chrome / Mobile Safari浏览器上打开它,这个错误可能会帮助你找出问题所在。如果可以将其下拉,那么问题很可能存在于代码中(可能是URL中的一个输入错误)。如果不执行GET请求,这将无法工作,但它至少可以帮助您确定您的手机是否可以到达您的服务器。
  3. Connect your device to your PC on USB and observe the request in a Safari/Chrome debugger's Network tab. You should probably be doing this all the time anyway, but it always seems to be my step #2.
  4. 将设备连接到USB上的PC上,并在Safari/Chrome调试器的网络选项卡中观察请求。无论如何,你应该一直这样做,但这似乎总是我的第二步。
  5. Set one breakpoint before your request is sent, another in the success handler and a third in the error handler. The request may be successful, but an error immediately after the response could be stopping your program. Or maybe the response text is not what you were expecting (a web page rather than a JSON stream.)
  6. 在发送请求之前设置一个断点,另一个在成功处理程序中,第三个在错误处理程序中。请求可能是成功的,但是响应之后的错误可能会导致程序停止。或者可能响应文本不是您所期望的(web页面而不是JSON流)。
  7. If you are doing a non-GET request (like you are here), you can try running your own AJAX request directly in the debugger console. If you duplicate the request exactly, it shouldn't give you any more information than watching the regular request in the network tab, but it does allow you to modify the request multiple times without recompiling your app. It can be a huge time-saver.
  8. 如果您正在执行一个非get请求(就像您在这里一样),您可以尝试在调试器控制台中直接运行您自己的AJAX请求。如果你准确地复制请求,它不应该比查看network选项卡中的常规请求提供更多的信息,但它确实允许你多次修改请求,而无需重新编译应用程序。

If your phone can't reach the resource at all (#1) then maybe you have a specialized local environment that your phone doesn't share. For example, you might be calling localhost or 127.0.0.1, which your phone will never reach (because these IP addresses have a special meaning on most networks and basically means 'me' or 'this computer'). Or maybe you're calling out to something like 192.168.xxx.xxx or 10.xxx.xxx.xxx, which your phone can't reach because it isn't on that private network (e.g if you are not on the same wifi). In this case, you have two options:

如果你的手机根本无法访问资源(第一条),那么你可能有一个你的手机无法共享的专门的本地环境。例如,您可能正在调用localhost或127.0.0.1,而您的手机将永远无法访问它们(因为这些IP地址在大多数网络上都有特殊的含义,基本上是指“我”或“这台计算机”)。或者你可能在喊192。168。xxx。xxx或10. xxx.xxx。xxx,你的电话无法到达,因为它不在那个私有网络上(e。g如果你不在同一个wifi上)。在这种情况下,你有两个选择:

  1. figure out how to expose those resources to outside requests, or
  2. 找出如何将这些资源公开给外部请求,或者
  3. get your phone into your network environment. In extreme cases, I've actually had to build my own DNS server, change my phone's networks settings to use that, and redirect requests going to specific domain names.
  4. 让你的手机进入你的网络环境。在极端情况下,我实际上必须构建自己的DNS服务器,更改手机的网络设置以使用它,并将请求重定向到特定的域名。

#2


0  

With Phonegap version "Cli-5.2.0", whitelist plugin need to be use to successfully make ajax call on Android. Add below mentioned line in config.xml to use whitelist plugin with Phonegap cloud build.

使用Phonegap版本的Cli-5.2.0,需要使用whitelist插件成功地在Android上调用ajax。在配置中添加下面提到的行。使用whitelist插件和Phonegap云构建。

<gap:plugin name="cordova-plugin-whitelist" source="npm" />
<access origin="*" />
<allow-intent href="*" />
<allow-navigation href="*" />

Check https://github.com/apache/cordova-plugin-whitelist for whitelist plugin description

查看https://github.com/apache/cordova-plugin-whitelist以获得whitelist插件的描述

#1


1  

The general steps I go through for checking problems like this on mobile are:

我在手机上检查这类问题的一般步骤是:

  1. The simplest thing you can do is try pulling up the resource via a regular browser window on your phone and see what's different. Just type in the URL and see what happens. If you can't pull it down on a generic Chrome / Mobile Safari tab, the error may help you figure out what the problem is. If you can pull it down, the problem likely resides in your code (a typo in the URL perhaps). This won't work if you aren't doing a GET request, but it may at least help you figure out if your phone can reach your server.
  2. 你能做的最简单的事情就是通过你手机上的一个普通浏览器窗口打开资源,看看有什么不同。只需输入URL,看看会发生什么。如果你不能在普通的Chrome / Mobile Safari浏览器上打开它,这个错误可能会帮助你找出问题所在。如果可以将其下拉,那么问题很可能存在于代码中(可能是URL中的一个输入错误)。如果不执行GET请求,这将无法工作,但它至少可以帮助您确定您的手机是否可以到达您的服务器。
  3. Connect your device to your PC on USB and observe the request in a Safari/Chrome debugger's Network tab. You should probably be doing this all the time anyway, but it always seems to be my step #2.
  4. 将设备连接到USB上的PC上,并在Safari/Chrome调试器的网络选项卡中观察请求。无论如何,你应该一直这样做,但这似乎总是我的第二步。
  5. Set one breakpoint before your request is sent, another in the success handler and a third in the error handler. The request may be successful, but an error immediately after the response could be stopping your program. Or maybe the response text is not what you were expecting (a web page rather than a JSON stream.)
  6. 在发送请求之前设置一个断点,另一个在成功处理程序中,第三个在错误处理程序中。请求可能是成功的,但是响应之后的错误可能会导致程序停止。或者可能响应文本不是您所期望的(web页面而不是JSON流)。
  7. If you are doing a non-GET request (like you are here), you can try running your own AJAX request directly in the debugger console. If you duplicate the request exactly, it shouldn't give you any more information than watching the regular request in the network tab, but it does allow you to modify the request multiple times without recompiling your app. It can be a huge time-saver.
  8. 如果您正在执行一个非get请求(就像您在这里一样),您可以尝试在调试器控制台中直接运行您自己的AJAX请求。如果你准确地复制请求,它不应该比查看network选项卡中的常规请求提供更多的信息,但它确实允许你多次修改请求,而无需重新编译应用程序。

If your phone can't reach the resource at all (#1) then maybe you have a specialized local environment that your phone doesn't share. For example, you might be calling localhost or 127.0.0.1, which your phone will never reach (because these IP addresses have a special meaning on most networks and basically means 'me' or 'this computer'). Or maybe you're calling out to something like 192.168.xxx.xxx or 10.xxx.xxx.xxx, which your phone can't reach because it isn't on that private network (e.g if you are not on the same wifi). In this case, you have two options:

如果你的手机根本无法访问资源(第一条),那么你可能有一个你的手机无法共享的专门的本地环境。例如,您可能正在调用localhost或127.0.0.1,而您的手机将永远无法访问它们(因为这些IP地址在大多数网络上都有特殊的含义,基本上是指“我”或“这台计算机”)。或者你可能在喊192。168。xxx。xxx或10. xxx.xxx。xxx,你的电话无法到达,因为它不在那个私有网络上(e。g如果你不在同一个wifi上)。在这种情况下,你有两个选择:

  1. figure out how to expose those resources to outside requests, or
  2. 找出如何将这些资源公开给外部请求,或者
  3. get your phone into your network environment. In extreme cases, I've actually had to build my own DNS server, change my phone's networks settings to use that, and redirect requests going to specific domain names.
  4. 让你的手机进入你的网络环境。在极端情况下,我实际上必须构建自己的DNS服务器,更改手机的网络设置以使用它,并将请求重定向到特定的域名。

#2


0  

With Phonegap version "Cli-5.2.0", whitelist plugin need to be use to successfully make ajax call on Android. Add below mentioned line in config.xml to use whitelist plugin with Phonegap cloud build.

使用Phonegap版本的Cli-5.2.0,需要使用whitelist插件成功地在Android上调用ajax。在配置中添加下面提到的行。使用whitelist插件和Phonegap云构建。

<gap:plugin name="cordova-plugin-whitelist" source="npm" />
<access origin="*" />
<allow-intent href="*" />
<allow-navigation href="*" />

Check https://github.com/apache/cordova-plugin-whitelist for whitelist plugin description

查看https://github.com/apache/cordova-plugin-whitelist以获得whitelist插件的描述