CashLee李秉骏
CashLee李秉骏

Reputation: 1048

How to post Data to a Remote Server (Cross-domain) with jQuery in PhoneGap?

Here are my codes :

function makeRequest() {
    //alert("inside makeRequest()");
    var settings = {
        type: "POST",
        url: URL + "?" + REQUEST,
        dataType: "json" ,
        error: function(XHR,textStatus,errorThrown) {
            console.log(XHR);
            console.log("textStatus: " + textStatus);
            console.log("errorThrown: " + errorThrown);
            //alert ("XHR="+XHR+"\ntextStatus="+textStatus+"\nerrorThrown=" + errorThrown);
        },
        success: function(data,textStatus) {
            $("body").append(data);
            console.log(data, textStatus);
        },
        headers: {
            "Access-Control-Allow-Origin" : "http://xxx.com",
            "Access-Control-Allow-Headers" : "X-Requested-With",
            "X_APNSAGENT_API_KEY" : app_key,
            "X_APNSAGENT_API_VERSION" : version,
            "X_APNSAGENT_API_TIMESTAMP" : timestamp,
            "X_APNSAGENT_API_SIGNATURE" : hash,
        }
    };
    $.ajax(settings);
}
makeRequest();

In phonegap wiki,it said :

"The cross-domain security policy does not affect PhoneGap applications. Since the html files are called by webkit with the file:// protocol, the security policy does not apply."

But how can I call this?

Besides,I am using jquery-1.6.4.js

I tried to find the solution for some days.Please help me and save me ... thanks

Upvotes: 3

Views: 3698

Answers (2)

CashLee李秉骏
CashLee李秉骏

Reputation: 1048

I solve this problem by myself.

At last.I found that jquery cause this problem.

Before ajax request.I add the code : $.support.cors = true;

Finally everything works well.

For the detail,you can go to my blog http://cashwordpress.sinaapp.com (But sorry I just post a Chinese text).

Upvotes: 1

Melvin Joseph Mani
Melvin Joseph Mani

Reputation: 1496

Here is the method I have used when I faced such an issue last month.

$.ajax({
            url:dataUrl,
            dataType: 'jsonp',
            success:function(response){
                //success call back
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                                //error call back
                        }
            }
    });

If you see the post by Remy Sharp at this link http://remysharp.com/2007/10/08/what-is-jsonp/, it will be more clear.

Hope it helped you.

Upvotes: 2

Related Questions