简体   繁体   中英

Unable to access data in Dojo JsonRest store, target is PHP

I am trying to copy the results.total example from the dojotoolkit.org reference guide for dojo/Store/jsonRest (v1.8) from here .

My jsonRest store target is a php page as I learned about here , and I added console.debug(data); which in firebug shows that the data is returned from php the way I think it should be: (sorry tried to show pic here but I can't)

The problem is that I can't figure out how to access the data? results.total.then is returning nothing, I don't think that function even gets run.

I tried store.get and that gives me a 404 error for the php url. I tried converting the store like this: newStore = ObjectStore({objectStore: store}); and then running a query on newStore, and this gives no error but also NO result.

I also get a 404 error if I try to query the store using the “foo=bar” syntax , in my case store.query ("ADDNUM='200'") but there are no errors if I use the store.query ({ADDNUM:"200"}) syntax.

My ultimate goal is to have the results show up in a dojo filteringselect, but for now I will settle for being able to get to them at all, even just console.log(result).
Very grateful for any help anyone can give!

require([
  "dojo/store/JsonRest", 
  "dojo/data/ObjectStore", 
  "dojo/store/Memory"
  ], function(JsonRest, Memory, ObjectStore){
    var store = new JsonRest({
    target: "scripts/AddressNumTest.php"
    });

var self = this;
var results = store.query({ADDNUM:"200"}).then(function(data){
  console.debug(data);  //results from this look okay to me
  results.total.then(function(total){
     console.log("Never gets here??");
     console.log("total results: ", total);
     console.log("go on and use data ", data, " with this ", self);
     });
  });

//store.query("ADDNUM='200'")  //this returns 404 error

//below gives 404 error           
//var results2 = store.get({ADDNUM:"200"}).then(function(newdata){
//console.log("from get: " + newdata);            
//});

});

Result when no Error but also no success in accessing data in store: GET ../scripts/AddressNumTest.php?ADDNUM=200 200 OK -- RESPONSE: {identifier: 'OBJECTID', label: 'ADDNUM', 'items':[{"ADDNUM":"136","OBJECTID":"307"},{"ADDNUM":"150","OBJECTID":"308"},{"ADDNUM":"200","OBJECTID":"8494"},{"ADDNUM":"210","OBJECTID":"8495"},{"ADDNUM":"220","OBJECTID":"2950"}]}

404 Error when using store.get: GET .../scripts/AddressNumTest.php%5Bobject%20Object%5D 404 Not Found RequestError: Unable to load scripts/AddressNumTest.php[object Object] status: 404

404 Error when I use "ADDNUM='200'" syntax: GET .../scripts/AddressNumTest.phpADDNUM=%27200%27 404 Not Found RequestError: Unable to load scripts/AddressNumTest.phpADDNUM='200' status: 404

I don't know if results is available in the closure or not, but I don't think you need it. The callback is passing in data , which looks correct to you in your console.debug(data) , so let me suggest this code.

var results = store.query({ADDNUM:"200"}).then(function(data){
  console.debug(data);  //results from this look okay to me
  console.log("You will get here!  :)");
  // I do not know what data looks like so this may or may not work.
  console.log("total results: ", data.total);
  console.log("go on and use data ", data, " with this ", self);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM