YUI 3: Setting request headers with DataSource.IO - ajax

I need to retrieve a JSON resource which requires HTTP Basic authentication. Therefore, I need to set a request header for a DataSource.IO object.
I see that the IO utility itself supports a header key in its configuration object. However, since I'm new to YUI, I can't figure out how to set this configuration value through the mediation of DataSource.
To be clear, I don't need help constructing a correct Authorization header, just getting YUI to send the headers I construct. Thanks much.

You can set IO config values via DataSource.IO's ioConfig config object:
var ds = new Y.DataSource.IO({
source: "script.php",
ioConfig: {
method: "POST",
data: "foo=bar",
timeout: 1000
}
});

Related

Fine Uploader Params Not Sent

I'm trying to use Fine Uploader 5.15.0, set up with multiple file fields & uploader instances (multiple: false is set on each) which all display and select files correctly. They are posting to a custom endpoint that is returning any parameters to me. Uploads are set to happen as soon as the file is selected, and file and QQ parameters are sent okay.
My problem is when I attempt to send additional data to the server along with the upload.
I have tried including my additional parameters in the endpoint of the request option, and as a params node added to the options variously as follows:
uploaders[1] = new qq.FineUploader({
element: document.getElementById("uploader-1"),
multiple: false,
request: {
endpoint: "default.cfm",
paramsInBody: false,
params: {
act: "action/processFile",
uid: 4747
}
}
})
Calls without any parameters available in either form or URL scopes.
Removing the params section and attempting to pass them via endpoint:
request: { endpoint: "default.cfm?act=action/processFile" }
Works fine, but obviously no additional parameters.
request: { endpoint: "default.cfm?act=action/processFile&uid=4747" }
Calls without any URL parameters.
request: { endpoint: "default.cfm?act=action/processFile&uid=4747" }
Calls with "act" available, but all others have their ampersand stripped out, so the parameter name becomes "amp;uid"
According to https://blog.fineuploader.com/include-params-in-the-request-body-or-the-query-string-479ac01cbc63 and other questions on here the first one should work. For the others obviously FineUploader is doing some additional processing on endpoint that is wiping out my parameters.
I'm missing something essential, can anyone educate me?
Thanks!

Include date field in to the header requests Web Api

Is it possible to enable date field in to http requests? I have an object on my client side:
let init = {
method: typeof method === 'string' ? method : 'GET',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Accept': 'application/json',
'Accept-Language': getLanguage()
}
The problem is I adding 'Date' : new Date() to the header server doesn't get any key-value pair (via WebApi). Also in network section of browser there is no above field. I've read some information this field is closed for any manipulation. As I understand I need to enable it for including not by hands. So, how can I tell to browser to send it?
Unfortunately, that's not possible once the browser is supposed to set the header, not you. If you were able to set the header, that would defeat the purpose of the security feature.
Also, if you try to force it you'll probably get the error:
Refused to set unsafe header "Date"
Once we try the request without setting this header, we'll see that the browser doesn't set it for you (only for response object, which is easier to manipulate).
Some alternatives:
Create custom headers and receive their values at the WebApi
Or even pass the value as a parameter (body POST, e.g)

sendAJAX data parameter in CasperJs

again, I got another problem with casperjs, now with sendAJAX function.
It says that sendAJAX has 5 parameters which are these followings :
url: The url to request.
method: The HTTP method (default: GET).
data: Request parameters (default: null).
async: Flag for an asynchroneous request? (default: false)
settings: Other settings when perform the AJAX request (default:
null)
So, it says the data method is object so, it should be filled with :
var data = new Object();
data.amount= 15;
and also with this one,
var data = {amount:15};
but there were no successful value send to my web service (always send 0 as value, but ajax request successful, even returning the json data) which has an url like this
"http://localhost:9000/TempCountryAmountREST/setCountryAmount"
It will be succeed if I direct bind my data variable to my url like this :
"http://localhost:9000/TempCountryAmountREST/setCountryAmount?amount="+amount
[UPDATE]
The TempCountryAmountREST is my controller name and setCountryAmount is my function inside my controller.
[UPDATE]
I forgot to include my usage of sendAJAX(), here is the code that I use :
return JSON.parse(__utils__.sendAJAX(wsurl, "POST" , data, false, { contentType: "application/json" }));
So how does I fill the data in the sendAJAX parameter?
Thanks in advance...
Sorry, I've found what the answer is.
I make some mistakes in contentType which I was set with contentType: "application/json" instead of contentType: "application/x-www-form-urlencoded" }
If we are looking about how ajax send the content from method send(), they were use x-www-form-urlencoded. See this for more detail
When we see through casperjs clientutils.js script, we should found how sendAJAX work.
On the `this.sendAJAX = function sendAJAX(url, method, data, async, settings) {
}
there are url construction logic which transformed our Object (if so) to x-www-form-urlencoded form. So that we need to set our contentType as application/x-www-form-urlencoded
Very well, thanks for your attention...

Making jQuery $.ajax call to Twitter API 1.1 search

Here is a very simple example of a call to Twitter's search API to get all tweets from a tag known to have tweets, #fml.
I believe I am correctly using the application-only authentication as explained here: https://dev.twitter.com/docs/auth/application-only-auth (see Step 3 for example of a call)
I am being asked for a solution that does not involve any server-side code so I am including the bearer code in the javascript, which isn't good to begin with, but....
I would expect this code to work. Instead it produces the error '400 (Bad Request)'. Any ideas?
$.ajax({
url: "https://api.twitter.com/1.1/search/tweets.json",
dataType: "jsonp",
data: "q=%23fml",
beforeSend: function (xhr) {
xhr.setRequestHeader ("Authorization", "Bearer XXmyBearerCodeXX");
},
success: function(json){ alert(json); }
});
EDIT 1 - Validated Twitter call
Using hurl.eu I was able to get a successful response from the API with the above query and Authorization header, so I assume this means my Twitter call is correct, just not set up correctly within jQuery.ajax(), but I just don't see what is missing.
You cannot set request headers using AJAX calls with dataType JSONP.
See this question: Set Headers with jQuery.ajax and JSONP?
The best solution is to use a server-side proxy to do the search for you. I know you are looking for a client only solution, but with this restriction, and with no way around CORS, this is how it seems to be done today for the Twitter API.
Edit It may be possible using a proxy like Yahoo's YQL if you don't have access to one.
on your severside create a jsp or servlet and from the client side make a JSON call to the .jsp/servlet and that will return back the json object to the javascript. In serverside use the twitter4j api.
sample code:
`
$.getJSON(http://localhost:8080/test.jsp?callback=?",
{
jspqueryStr : queryStr,
jspgeocodeStr : geocodeStr,
lat:latStr,
lan:lngStr,
radius:radiusStr,
}, displayResult);
//This function returns the data as json object from server.
function displayResult(data) {}
In the jsp the code is like below
<%
String jspqueryStr = request.getParameter("jspqueryStr");
String jspgeocodeStr = request.getParameter("jspgeocodeStr");
String diseasename = request.getParameter("jspqueryStr");
String lat = request.getParameter("lat");
String lan = request.getParameter("lan");
String radius = request.getParameter("radius");
Gson gson = new Gson();
String json = gson.toJson(tweetList);
json = request.getParameter("callback") + "(" + json + ");";
out.println(json);
public List<Status> searchstream(){
//here all the twitter4j api code to get the data
retrun tweetList;
}
%>
`

Content loading with jsonp

I am a beginner at sencha touch2. i am trying to create a some app which contains a blog view. the code of the blog is given below. when i launch the app, the content fails to load, giving these errors. i am using wamp for localhost.
XMLHttpRequest cannot load " http://secureclick-media-maynemyltf.netdna-ssl.com/Extensions/rjs/c2.js". Origin< http://localhost> is not allowed by Access-Control-Allow-Origin.
XMLHttpRequest cannot load " http://api.yontoo.com/GetClientData.ashx?key=null&id=47a8564d-d089-4195-9564-72f107ea1c56&loc=http%3A//localhost/GS/&apps=bestvideodownloader,ezLooker,pagerage,buzzdock,toprelatedtopics,twittube". Origin <http://localhost> is not allowed by Access-Control-Allow-Origin.
Ext.define('GS.view.blog',
{
extend:'Ext.navigation.View',
xtype: 'blogpanel',
config:{
title: 'Blog',
iconCls: 'star',
items:
{
xtype:'list',
itemTpl:'{title}',
store:
{
autoLoad: true,
fields:['title','author','content'],
proxy:
{
type:'jsonp',
url: 'https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://feeds.feedburner.com/SenchaBlog',
reader:
{
type:'json',
rootProperty:'responseData.feed.entries',
}
}
}
}
}
});
You're not calling a JSONP service but a JSON one. You can detect it by calling your URL from your browser and see the content isn't starting by a method call.
So you're not bypassing Cross-Domain protections.
You can't just tell the server you want it to answer in JSONP : it must be ready to make such an answer.
And your browser won't let you access from another domain a server answering in json and not having set a header specifying he accepts this cross-domain request. Read this.
EDIT :
You may call this service using JSONP : you just have to specify a callback at the end of the URL.
In addition to this response format, the protocol also supports a
classic JSON-P style callback which is triggered by specifying a
callback argument, which directs the API to deliver the JSON object as
an argument to the specified callback.
Example from the documentation :
'https://ajax.googleapis.com/ajax/services/feed/find?v=1.0&q=Official%20Google%20Blogs&callback=processResults'

Resources