簡體   English   中英

PhoneGap Ajax無法正常工作

[英]PhoneGap ajax not working

我正在使用file://協議開發phoneGap應用。 使用ajax時,我一直收到此錯誤。 我的服務器頁面上有標頭(“ Access-Control-Allow-Origin:*”)。 但是,無論我做什么,都無法得到ajax響應。 我該怎么辦?

無法加載文件:/// C:/test4/www/trackmyrunning.byethost22.com:僅協議方案支持跨源請求:http,數據,chrome,chrome擴展名,https。

    $("#b").on('click',function(){
            //pull vars 

            var username = $('#username').val();
            var password = $('#password').val();

            $.ajax({
                url: "trackmyrunning.byethost22.com",
                type: 'POST',
                success: function(data)
                {
                    $("#loginMessage").html(data);
                },
                error: function(xhr, status,error)
                {
                    alert(xhr.status + status + error);

                }

            });
            //request for username
           /* $.get("trackmyrunning.byethost22.com/index.", {user:username,pass:password}).done(function(data){
                $("#loginMessage").html(data);
            }); */

        });

我也有。 我嘗試將其修改為,但沒有運氣。 我在警報中收到的錯誤消息是0錯誤,基本上是訪問被拒絕做來越過原點。

使用手機差距和春季網絡服務開發移動應用程序時,我遇到相同的問題。

請記住,您必須傳遞此處缺少的用戶名和密碼憑據

然后設置標題

Access-Control-Allow-Origin標頭是必須在服務器端設置的響應標頭。 也就是說,如果相應的服務器未發送此標頭,則由於跨服務器(域,子域和協議)的侵害,瀏覽器將阻止您向該服務器請求信息。

僅當原始頁面由同一服務器(域,子域和協議)提供服務時,它才允許請求。

       Modify your js file like this ,

        $("#b").on('click',function(){
        //pull vars 

        var username = $('#username').val();
        var password = $('#password').val();

        $.ajax({
            url: "trackmyrunning.byethost22.com",
            type: 'POST',
            data: { username : username , password : password } ,
            headers: {
                'Access-Control-Allow-Origin': '*'
            },
            crossDomain: true,
            success: function(data)
            {
                $("#loginMessage").html(data);
            },
            error: function(xhr, status,error)
            {
                alert(xhr.status + status + error);

            }

        });

        });

如果問題仍然存在,則必須修改服務器端代碼。

您可以按照以下方式在java響應(java后端)中設置標頭,

try 
{ 
res.setContentType("application/json");
res.addHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "*");
res.setHeader("Access-Control-Allow-Headers", "X-Requested-      With,Host,User-Agent,Accept,Accept-Language,Accept-Encoding,Accept-Charset,Keep-Alive,Connection,Referer,Origin");
res.setHeader("Access-Control-Max-Age", "8080");
res.setCharacterEncoding("utf-8");
res.getWriter().write(response);
 } 
 catch (IOException e) 
 {

  }

按照以下方式,您可以在php(后端php)中設置標頭,

   header('Access-Control-Allow-Origin: *'); 
header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token    , Authorization');

您缺少的是URL的方案,而在Phonegap應用程序中,如果沒有為URL提供任何方案,則使用默認方案( file:/ /)。

更改您的網址以包含http://https:// (根據您的配置)

例:

url: "http://trackmyrunning.byethost22.com"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM