Search code examples
ajaxjquerycross-domainjsonpuserscripts

How to send and retrieve cross-domain ajax data in userscript


I use this code to store and retrieve ajax data via http://openkeyval.org/

 $.ajax({    /*   send data    */
        url: "http://api.openkeyval.org/store/",
        data: "test-key-data=" + JSON.stringify([123,456]),
        dataType: "jsonp",
        success: function(data){
            console.log(data);
        }
 }); 

$.ajax({     /*   retrieve data     */
        url: "http://api.openkeyval.org/test-key-data",
        dataType: "jsonp",
        success: function(data){
            console.log(data);
        }
 }); 

everything work fine in Chrome javascript console but in userscript I get error like this

Uncaught ReferenceError: jQuery110208458673823624849_1375932537303 is not defined

I try to use GM_xmlhttpRequest to retrieve data like this

GM_xmlhttpRequest({
    method: "GET",
    url: "http://api.openkeyval.org/test-key-data",
    onload: function(response) {
        console.log(response.responseText);
    }
});

but it seem like openkeyval doesn't accept data via POST/GET method and log result was like when you access it directly from url of browser like this

{"error":"not_found","documentation_url":"http://openkeyval.org/"}

I include jQuery and it work fine with this code
// @require http://code.jquery.com/jquery-latest.min.js

I try to use Greasemonkey/jQuery XHR bridge with out change other code by like this
// @require http://courses.ischool.berkeley.edu/i290-4/f09/resources/gm_jq_xhr.js

and try use openkeyval official javascript library with code like this
// @require http://cdn.openkeyval.org/statics/openkeyval.packed.js
and retrieve data with code like this

var ourCallback = function(value, key) {
  console('The value of ' + key ' + is ' + value);
};
window.remoteStorage.getItem('test-key-data', ourCallback);

still got error ERROR: Unexpected string

Please help, I mess with it more than 10 hours.
Thank you so much.


Solution

  • It look like $.ajax always trigger error event function
    but GM_xmlhttpRequest can retrieve mistype data,
    so I try looking for dataType: "jsonp" in GM_xmlhttpRequest and I got that jsonp header content-type is "application/javascript" OR "application/json" and the first one work well.

    my new code for retrieve data look like this

    GM_xmlhttpRequest({
        method: "GET",
        url: "http://api.openkeyval.org/test-key-data?nocache=" + new Date(),
        headers: {  
             "Content-Type": "application/javascript"
        },
        onload: function(response) {
            console.log(response.responseText);
        }
    });
    

    and retrieve data using $.ajax even it always trigger error event function but it still send data.
    I try both content-type on GM_xmlhttpRequest and still not work.

    my code to store data look like this

    $.ajax({    /*   send data    */
            url: "http://api.openkeyval.org/store/",
            data: "test-key-data=" + JSON.stringify(myVarObject),
            dataType: "jsonp"
     });