2015-12-31 32 views
2

我想運行我的phonegap應用程序在漣漪模擬器和調用方法從webservice.asmx在jQuery中使用ajax方法,但得到了Cors錯誤:在服務器端沒有'訪問控制允許來源'標題存在於所請求的資源ajax jquery phonegap

XMLHttpRequest cannot load https:\rippleapi.herokuapp.com\xhr_proxy?tinyhippos_apikey=ABC&tinyhippos_rurl=http%3A//www.my-domain.com/WebService.asmx/selectData. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http:\localhost:4400' is therefore not allowed access. The response had HTTP status code 503.

  1. 已經放棄CORS(web.config中):

<system.webServer> <defaultDocument> <files> <clear /> <add value="index.aspx" /> <add value="WebService.asmx"/> </files> </defaultDocument> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*"/> <add name="Access-Control-Allow-Headers" value="Content-Type"/> </customHeaders> </httpProtocol> </system.webServer> <system.web> <webServices> <protocols> <add name="HttpGet"/> <add name="HttpPost"/> </protocols> </webServices> <customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/> </system.web> <system.serviceModel>

  • 我的AJAX方法:

    $阿賈克斯({
    類型: 「POST」,
    跨域:真實,
    網址: 「http://www.my-domain.com/WebService.asmx/selectData」,
    數據:JSON.stringify(campaignData),
    contentType:「application/json;字符集= UTF-8" ,
    數據類型: 「JSON」,
    成功:函數(MSG)
    {
    變種響應= msg.d;
    變種resultLoop = $ parseJSON(響應);
    控制檯.LOG(響應)
    },
    錯誤:功能(XHR,ajaxOptions,thrownError)
    {
    $ .mobile.loading( '隱藏');
    警報( 「狀態:」 + xhr.status + 「thrownError:」+ thrownError +「ajaxOption:」+ ajaxOptions);
    }
    });

  • 不能夠解決這個,不知道我做錯了或失去了一些東西 在那裏我有,以便與服務器進行通信,並獲取數據的代碼改變。

    +1

    您是否在網絡工具中查看了服務器的實際請求/響應,以查看來自服務器的響應實際上有哪些標頭? – jfriend00

    回答

    1

    在紋波模擬器上運行phonegap應用程序,更改跨域代理設置爲禁用,它的工作。

    +0

    如果您能夠回答您自己的問題,請將您自己的答案標記爲已接受的答案。 – twernt

    -1

    可以使用JSONP的跨域AJAX resuest爲:

    $.ajax({ 
    type:"POST", 
    url: "http://www.my-domain.com/WebService.asmx/selectData", 
    data: JSON.stringify(campaignData), 
    contentType: "application/json;charset=utf-8", 
    dataType:"jsonp", 
    success: function(msg) 
    { 
    var response=msg.d; 
    var resultLoop=$.parseJSON(response); 
    console.log(response) 
    }, 
    error: function(xhr, ajaxOptions, thrownError) 
    { 
    $.mobile.loading('hide'); 
    alert("status :"+xhr.status +" thrownError :"+ thrownError +" ajaxOption : "+ ajaxOptions); 
    } 
    }); 
    

    你有一個字符串類型的服務器側添加的Web方法「回調」一個參數一兩件事:

    selectData(string callback){ 
    var JSONString = new JavaScriptSerializer().Serialize(""); 
    //JSONString is a json format 
    return callback+"("+JSONString + ")"; 
    } 
    

    欲瞭解更多詳細信息,您可以參考http://www.niceonecode.com/Q-A/JAVAScript/AJAX/Cross-domain-ajax-request-to-a-json-file-using-JSONP/20154

    +0

    嗨, 我不想使用JSONP,因爲它在Android設備上工作正常,我想運行在漣漪模擬器在鉻。 – user4582995

    0

    您可以添加此方法(郵局,刪除等)被允許在服務器端代碼,或者您可以使用Chrome插件Access-Control-Allow-Headers。像在php

    header("Access-Control-Allow-Origin: http://localhost:8080"); 
    header("Access-Control-Allow-Methods: GET, POST, OPTIONS, DELETE, PUT"); 
    header("Access-Control-Allow-Credentials: true"); 
    
    相關問題