質問

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.

役に立ちましたか?

解決

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"
 }); 

他のヒント

Add this into $.ajax({...})

crossDomain: true;

It is because by default cross domain ability is disabled. See http://api.jquery.com/jQuery.ajax/

EDIT:

Sometimes there will be a issue with different charset between local script and remote script. Try using:

scriptCharset: "utf-8";

Also look at JQuery AJAX is not sending UTF-8 to my server, only in IE

Elaborating my comment

The reference is to the callback function generated by jquery. It Sounds to me the way you invoke your userscript unloads the jquery functions before the callback is executed. Perhaps you use a link and forgot the preventDefault?

If you ajax and have

 $("#linkid").on("click"

or

 $("#formid").on("submit"

it is MANDATORY to continue like this:

           ,function(e) {
    e.preventDefault();

Otherwise the link is followed or the form is submitted which may not have any visible effect, but the asynchronous scripts have been (partially) unloaded unless the form and link has a target other than the current window

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top