PDA

View Full Version : Try to get JSON from my search engine with JsonStore



vincentp
11 Sep 2008, 9:09 AM
Hello,

I'm new in the ExtJS community !
After read few documentation and examples, I would like to create my first application with ExtJS. I need to display JSON datas with XTemplates. Datas come from a personal search engine and could be return in JSON or XML. I chose to display them with JSON.

My code is the following :

<script type="text/javascript">
Ext.onReady(function(){

var tpl = new Ext.XTemplate(
'<tpl for="."><p>ID: {id}</p></tpl>'
);

var ds = new Ext.data.JsonStore({
root: 'response',
fields: [
{name: 'id'}
],
proxy: new Ext.data.ScriptTagProxy({
url: 'my_url'
})
});

ds.on('load', function(){
tpl.overwrite('global', ds.reader.jsonData.response.docs, true);
});

ds.load();

});
</script>

<div id="global">

</div>

And my JSON is like that :

{
"responseHeader":{
"status":0,
"QTime":0,
"params":{
"indent":"on",
"start":"0",
"q":"*:*",
"wt":"json",
"version":"2.2",
"rows":"10"}},
"response":{"numFound":163,"start":0,"docs":[
{
"id":"0711719721123",
"country":["uk"],
{,
...
But it is not working : I get an 'Invalid label' (very popular error here ;)) in the second line :begin of "responseHeader".

Then, I tried with an example url http://extjs.com/forum/topics-remote.php
I modified few vars in my javascript code and it was working.

Something is missing in my javascript or JSON?
Could you give me some informations?

Thank you,
Vincent

devnull
11 Sep 2008, 9:28 AM
Do you understand the requirements for ScriptTagProxy, and the special circumstances that require its use?

vincentp
11 Sep 2008, 12:19 PM
I understood the request url should be in external domain (my server).
Then, I didn't understand what mean 'The content passed back from a server resource requested by a ScriptTagProxy must be executable JavaScript source code'.
But I can't change anything in my server. If I can't use this function, can you help me to find the right way (the function) ?

Thank you,
Vincent

devnull
11 Sep 2008, 12:31 PM
If you are requesting data from the same server that the javascript was served from, a normal httpProxy will suffice.

vincentp
11 Sep 2008, 12:51 PM
Yes, I saw the possibility, but I'm requesting an external server (not in the same domain).
It is working with http://extjs.com/forum/topics-remote.php (extjs example), there is a difference between this page and mine?

devnull
11 Sep 2008, 1:16 PM
What that means is that the result must be in a form that will result in a function when it is eval'd. And actually it is a specific function name, one that the scriptTagProxy provides to the backend (as the 'callback' param).
What you are missing in that example is that the live search comobox example sends a url that looks more like this: http://extjs.com/forum/topics-remote.php?start=0&limit=10&callback=stcCallback1001
Examine that result and you will see how it is different from yours.
The short answer here is that it is impossible to make a scriptTagProxy work unless the server code is modified to return the correctly formatted json. However one option you can use is to make a simple redirector on your server; that is a script that all it does is make a request to this foreign server and echo the results back to your client.

vincentp
12 Sep 2008, 12:35 AM
Hello,

Thanks for your answer! That was my thinking, you can't access with ajax to a server easily... Well, I saved finally my datas directly to a file called 'datas.json' to use 'HttpProxy'.
But I got the following javascript error :
'not well-formed' (in first character).
Maybe my file is not recognize as a json file? I have to add a 'JSON' header using a language like PHP?


var ds = new Ext.data.JsonStore({
root: 'response',
fields: [
{name: 'id'}
],
proxy: new Ext.data.HttpProxy({
url: 'solr.json'
})
});

And JSON didn't change.


Thank you,
Vincent

devnull
12 Sep 2008, 6:46 AM
Perhaps, it depends on the server technology being used.
It would be best to look at the json as it is received by the client, by looking in the firebug console.

marianoc
2 Mar 2009, 10:35 AM
hola vincentp (http://extjs.net/forum/member.php?u=48103) , tengo el mismo problema, no puedo conectarme a Solr desde un componente ExtJs, żż Pudiste solucionarlo??

hola vincentp (http://extjs.net/forum/member.php?u=48103) , i can some problem, i can't connect to Solr from any ExtJs's component, Can did you solver this problem???

gracias,
thanks,
mariano.

vincentp
2 Mar 2009, 10:41 AM
hello,

I didn't find the solution... I switch to another library (jQuery).

marianoc
2 Mar 2009, 11:38 AM
ok, thanks.

Fredric Berling
23 Nov 2009, 9:09 AM
I noticed you must be using the SolR search engine.
Here is the solution to your problem. Works like a charm. Too bad you switched already, but maybe someone finds this useful :D

Solr accepts the parameter "json.wrf" wich wraps the result in a function name.


proxy: new Ext.data.ScriptTagProxy({
url: 'http://solr.mydomain.com:8080/solrdev/select/?',
method: 'GET',
callbackParam:'json.wrf'
}),
baseParams: {
q: 'test',
start:'0',
version:'2.2',
rows:'10',
indent:'on',
sort:'subject asc',
wt:'json'
},
reader: new Ext.data.JsonReader({
root: "response.docs",
id: "id",
idProperty: "id",
successProperty: "success",
totalProperty: "totalcount",
fields: ["subject", "id"]
}),