ChemDoodle Ajax Incompatibility with Pollen.js

2019-09-15 09:09发布

问题:

I'm trying to use iChemLabs cloud services from a html5 web worker. Normally the cloudservices requires jQuery but I can't import that into a web worker so I'm using Pollen instead with a ChemDoodle Web Components library with which I have stripped out the document-related things.

jQuery.Hive.Pollen provides a nice ajax function very similar to jQuery, but I can't seem to get it to work at all. I know this problem will be tricky to solve considering that Access-control-headers need to be set to allow any of you to actually find the solution. However, I'm a beginning javascript programmer and I was wondering if my two weeks of frustration is actually a small difference. I am trying to invoke the following function:

var cloudmolecule;

ChemDoodle.iChemLabs.readSMILES('N1(C)C(=O)N(C)C(C(=C1N1)N(C=1)C)=O', function(mol){
    cloudmolecule = mol;
});

Here is a link to the library code I am using, see the 'q.ajax' call and substitute jQuery = q for p = q (p is for pollen) in that block of code.

Right now I'm just trying to get the ajax call to work in an ordinary block of javascript with the plan to migrate to a web worker later.

If anybody could point out the problem to me I would be extremely grateful.

回答1:

solved! turns out iChemLabs rejects these two extra headers that pollen creates:

_xhr.setRequestHeader("X-Requested-With", "Worker-XMLHttpRequest");
_xhr.setRequestHeader("X-Worker-Hive", "Pollen-JS" );

Simply comment them out

Also, Pollen ajax seems to return a JSON object containing the data in JSON format AND as a string, so do

o = JSON.parse(data.string)//data is the parameter to the callback function

The reduced ChemDoodle library (without document-related methods) will work like a charm with pollen ajax.