I wanna know if it's possible to call php api like in extjs 3.x.x.
Let me explain, I have a store and I want to get data from php :
Ext.define('KY.store.watcherStore',{
extend: 'Ext.data.Store',
model: 'KY.model.WatcherModel',
proxy: {
type: 'ajax',
url : '/admin/rest/ksThresholdWatcher/loadAllRef',
//reader: {
// type: 'json'
//},
}
});
The url param calls ksThresholdWatcher.php method loadAllRef (I have a php rest api);
but it never calls my php script. I'm stuck, I don't now how to do, all examples I see set url param like this url:'myPHP.php'.
How can I make it work my way ?
You need to specify that the your proxy is type: 'rest' in the config. Here is an example the restful way. This also has some good pointers on proxy usage.
You can use the api proxy config item in 4.x.x to specify different sources for the different request operations (CREATE, UPDATE, DESTROY, READ).
Related
I'm following this article to mock response in dojo.
My mocker is very similar to the one in the article except this:
registry.register(/\/testIntern/, function (url, options) {
return when({
value: "Hello World"
});
In my understanding, this should map to any request that contains "/testIntern" on the address.
My testcase is quite simple:
// similar to example
var testRest= new Rest("/testIntern", true);
testRest("").then(lang.hitch(this, function (data) {
assert.deepEqual("Hello World", data.value, "Expected 'Hello World', but got" + data.value);
}));
It really should be quite simple. But when I run this test, I got 404 Not Found. It looks like the REST call in the test doesn't try to use the mocking service. Why?
You are generally correct in your thought that registering a URL with dojo/request/registry should pass anything referencing that URL via dojo/request through your handler.
Unfortunately, dojo/store/JsonRest uses the dojo/_base/xhr module which uses dojo/request/xhr directly, not dojo/request. Any registrations created with dojo/request/registry (and any setting of defaultProvider) will unfortunately be lost on JsonRest.
You might want to have a look at dstore - its Rest store implements the same server requests as dojo/store/JsonRest but it uses dojo/request instead of being hard-coded to a specific provider. (dojo/request defaults to dojo/request/xhr in browsers anyway, but can be overridden via dojoConfig.requestProvider.) dstore contains adapters for translating between dstore's API and the dojo/store API, if you need to use it with widgets that operate with the latter.
In relation to this question, How do I fully intercept AND requeue http requests using dojo
I am interested in something similar, would dojo/aspect better suit?
Looking at the documentation here, http://dojotoolkit.org/reference-guide/1.9/dojo/aspect.html The example given shows how to intercept dojo.xhr, but dojo.xhr is deprecated since 1.8??. and replaced by dojo/request. For the new API, dojo/request/notify is recommended by dojo documentation.
I am confused which one to use or should I use both methods? So I can cover all cases where some legacy codes are still using the old dojo.xhr API.
The other complication I can think of for using both methods, may be both methods are called for the same xhr request, thus duplicating the work.
dojo.xhr is deprecated in favor of dojo/request/xhr, the documentation for which you can find here.
You can still use dojo/aspect as in the example. I've created a fiddle that does just that. The relevant code follows.
require([
'dojo/request/xhr',
'dojo/aspect'
], function(xhr, aspect) {
aspect.before(xhr, 'post', function() {
console.log('before POST');
});
// use xhr.post here...
});
If you still want to cover legacy code that uses dojo.xhr, you can extract the function you use in aspect and pass it to both objects:
function beforeXhr() {
// ...
}
aspect.before(dojo, 'xhr', beforeXhr);
aspect.before(xhr, 'post', beforeXhr);
I haven't figured out how to use dojo/aspect on functions that are returned to the require callback, which means you'd have to repeat the aspect calls for 'GET', 'PUT', 'DELETE', etc. If anyone knows how to use aspect with a function object as in this example, I'd love to know.
I'm trying to do a POST to a service running on localhost with jQuery AJAX, but it keeps returning status code 0 even after I've set jQuery.support.cors = true. I can also navigate to my WCF REST service successfully from my browser. This is what my JavaScript looks like:
<script>
jQuery.support.cors = true;
$(document).ready(function(){
$.ajax({
type: "POST",
url: "http://localhost:8000/Test",
data: '{"test":"test"}',
contentType: "application/json",
dataType: "json",
success: function (msg) {
alert('success');
},
error:function(x,e){
if(x.status==0){
alert('error 0');
}
}
});
});
</script>
Does anyone know what could be causing this? I should also mention that I can't POST to anything on localhost using jQuery.
According to Fiddler, the JSON data is not sent, and a HTTP OPTIONS is done instead of a POST.
try this
var dataObj = {test:"test"};
var json = JSON.stringify(dataObj);
then in your ajax call
data: json,
I didn't want to spend anymore time on this issue, so I resorted to using raw HTML form POST as the usage of JSON wasn't essential in my case.
For anyone else having the same issues outlined in the original post, see this thread for an explanation and a solution: Problem sending JSON data from JQuery to WCF REST method
To summarize, your service needs to be able to handle the HTTP OPTIONS method if it is expected to respond to cross domain calls.
You should use a tool like network monitor etc. to see if the browser is asking the server for the allowed headers (using the OPTIONS header request), you may need to supply the correct headers in an OPTIONS response before the actual request is sent to the server (see the article at the bottom).
Also, you could try adding this to the actual call or the ajaxSetup, as you will need to tell the browser to send credentials and allow the cross domain call (I know someone else already mentioned 'crossDomain'):
$.ajaxSetup({
crossDomain: true,
xhrFields: {
withCredentials: true
}
});
Have a read of this if you get time too.. https://developer.mozilla.org/en/http_access_control
So, when the request is cross domain, jQuery will send your post request as a get request anyways.
Are you accessing "localhost" in the URL but then your application is sending the requests to the local IP of your machine instead of localhost? Because that's technically cross-domain, which means that you won't receive the request in the expected manner.
E.g. (just tested this locally)
Visiting my local site at:
http://localhost/test/
A form on the site submits to my local ip address instead of localhost via $.post():
<form action="http://10.0.0.17/test/" method="post">
....[form stuff...]
</form>
This is a cross-domain request
If you're calling $.post() or jquery's ajax() call set to post, it automatically moves your parameters from the post body into the query string.
If you ARE accessing local host, try hitting the site via whatever address your jquery post() method is using as the domain and see if that helps.
See more on cross-domain policies:
http://en.wikipedia.org/wiki/Same_origin_policy
Send the data as an Object literal instead of a string
data: '{"test":"test"}',
to
data: {test:"test"},
I am new to sencha touch can any one help me, what is the way to get data from server. I didn't understand clearly. Please help me. In need to display the values from server to grid. I dont how to do that.Now I have added the values directly to grid store.
You should dive deeper into two aspects of Sencha Touch: models and stores. A model is simply a representation of an object. For example, you could define 'User' as a model with the fields 'id', 'username', 'email'. Stores can be seen as collections of model-objects.
The beauty of Sencha is that the models / stores can be given a proxy parameter. There are various proxies available, but you should choose the one that suits application the best. For example, the AjaxProxy sends a request to the server to load the data. Once your store is loaded (please not that AJAX calls are made asynchronous) you can populate it in for exampe a List or NestedList.
Some example code, directly taken from the docs:
Ext.regModel('User', {
fields: ['id', 'name', 'email']
});
//The Store contains the AjaxProxy as an inline configuration
var store = new Ext.data.Store({
model: 'User',
proxy: {
type: 'ajax',
url : 'users.json'
}
});
store.load();
you can use this tutorial.
it's very useful
http://programmersgoodies.com/how-to-parse-xml-response-with-sencha-touch#comment-27
Download the SDK of sencha touch2 and then you can refer example which is present in sencha sdk for using the servers. This is important to use web services for getting data from servers.
Just refer to the below link.
sencha-touch-2.0.1.1\examples\ajax
I'm attempting to mock the response of a dojo xhr request, but I haven't found a good solution.
Ideally, I'd like to see a solution similar to the jQuery mockjax plugin where I can set a specific call based on a url, e.g.:
$.mockjax({
url: '/restful/fortune',
responseTime: 750,
responseText: {
status: 'success',
fortune: 'Are you a turtle?'
}
});
My initial thought was to utilize the "/dojo/io/send" channel, but I haven't been able to get a modified response to be loaded after modifying the dojo Deferred object.
The other thought is to use a pass-through method that would determine if an actual xhr request should be made, e.g.:
function xhrRequest(xhrArgs) {
if(shouldMock) {
var fakeReturnJson = dojo.toJson({
howdy: "that's odd!",
isStrange: false
});
return fakeReturnJson;
} else {
dojo.xhr(xhrArgs);
}
}
Can someone tell me the best way to go about mocking dojo xhr calls?
Thanks!
It's an old question, but I think you should do your mocking using Sinon.js
However you will need to put the following:
has: { native-xhr2: false }
into your dojoConfig for it to work in 1.8
I haven't heard of any Dojo specific libraries similar to Mockjax. But what I think you could try is use Mockjax with Dojo. This should be pretty easy to do since all you'll have to do is use JQuery during development only for testing with Mockjax and then remove it once development is complete.
I use your second suggestion. Currently, I have a transport layer (simple js class) and 2 implementations (XhrTransport and MockTransport). I then switch in which I need without changing the widget code.
Widgets call the server with:
Controller.send(aServerCall);
where aServerCall is a simple value object with the server endpoint, params and callback.
This way, you can add nice things to the controller that will apply to all server calls (such as logging, analytics, generic error handling...) and also mock out the entire server when doing unit tests.
For the MockTransport, I simply return canned json data from static .js files in the format that the widget expects.