I'm trying to create a function, using an API, to get the definition in french of a word.
I'm using this API:
"http://www.igrec.ca/project-files/wikparser/wikparser.php?word="
+ word +
"&query=def&count=1&lang=fr"
This url returns one definition in plain text of the word entered.
e.g: http://www.igrec.ca/project-files/wikparser/wikparser.php?word=manger&query=def&count=1&lang=fr
How does one manage to get this text? I looked at similar questions, some mention Ajax / xmlHttpRequest but I'm pretty lost.
Thanks
P.S: I don't mind using jQuery or some other technics as long as I understand what I'm doing.
The basic jQuery AJAX call can be done like this ... (using your data).
$.ajax({
type: "GET",
url: "http://www.igrec.ca/project-files/wikparser/wikparser.php",
dataType: "text",
data: {
word: word,
query: "def",
count: 1,
lang: "fr",
},
success: function(data) {
// Do something
}
});
Basically, you are performing a GET, at the URL.
The data type you expect back is text.
The data gets "transformed" into the url format from your question, then there is a success function to handle the data received.
Related
If I submit a form using POST, the ASP.NET server can access each value by name. However, if I do it with javascript like:
$http({
method: "POST",
url: TDSV.ROOT_PATH + "/themes/" + data.id,
params: { "xHttpMethodOverride": "PUT" },
data: { "newContent": JSON.stringify({ properties: data.json.properties, "otherInfo": "hello world" }, null, "\t") },
cache: false
});
It is all squashed together into a giant stream that I have to parse through. I have no interest in sending files this way. I just want to separate the strings by name. Is there a way to do this?
It looks like you are using WebForms, if that´s the case you must send the data in a different way.
Here is a tutorial, please take a look and let me know if that helps:
http://www.bennadel.com/blog/2615-posting-form-data-with-http-in-angularjs.htm
I am attempting to teach myself some JQuery/REST/Google API by putting together a simple page that does the following:
Authenticates with Google
Validates a token
Gets a list of Google Calendars for the user
List events for a selected calendar
Add an event for a selected calendar
I have #1 through #4 working (although no doubt in an ugly manner), and am getting tripped up on #5. Here's the JQuery ajax call:
var url = 'https://www.googleapis.com/calendar/v3/calendars/[MY_CALENDAR_ID]/events?sendNotifications=false&access_token=[GOOGLE_API_TOKEN]';
var data = { end: { dateTime: "2012-07-22T11:30:00-07:00" }
, start: { dateTime: "2012-07-22T11:00:00-07:00" }
, summary: "New Calendar Event from API"
};
var ajax = $.ajax({ url: url
, data: data
, type: 'POST'
}).done(addEventDone)
.fail(function (jqHXR, textStatus) {
console.log("addEvent(): ajax failed = " + jqHXR.responseText);
console.log(jqHXR);
});
The results are a global parseError: "This API does not support parsing form-encoded input.". The first four steps are all using GET ajax calls, so I'm not sure if that is what is tripping me up.
Here's the API in question: https://developers.google.com/google-apps/calendar/v3/reference/events/insert
I think I may be doing things the long and hard way, and my new approach is to tackle this using the javascript API instead of going straight at it with manual JQuery and REST. This is the approach I am attempting going forward http://code.google.com/p/google-api-javascript-client/wiki/Samples#Calendar_API, although I would still love to use this as a learning opportunity if there is something simple I am screwing up in the code above.
Thanks for any help, insights, pointers, etc. I will post updates if I make any progress using the javascript API.
Interestingly, I just answered a similar question here. Your intuition to use Google's JS client library is a good one. It's going to handle OAuth 2 for you, which is a requirement if you're going to do any manipulation of the Calendar data.
My other answer has both a link to a blog post that I authored (which demonstrates configuration of the client and user authorization), as well as an example of inserting a Calendar event.
you need to set contentType: "application/json", to do JSON.stringify for your data and method : 'POST'
var ajax = $.ajax({
url: url,
contentType: "application/json",
data: JSON.stringify(data),
method : 'POST',
});
I have a simple ajax call like this:
$.ajax({
url: u, type: "POST", dataType: "json",
data: data,
success: function (d) { response($.map(d, function (o) { return { label: o.Text, value: o.Text, id: o.Id} })); }
});
It is part of an tb autocomplete that does not work on only one view.
The reason it does not work is that instead of json, it makes jsonp request (by sniffing I saw that it calls passed url with ?callback=jQueryxxxxxxxxx), and success function is never called because jquery packs it into anonymous function whose name is passed in callback argument, and server returns standard json (I don't want to use jsonp as it is POST request and NOT cross-domain request). I checked, both current view url and this u for ajax url argument are on http://localhost:8080/myapp/areax/..., so I don't see why jQuery makes JSONP request here.
EDIT:
View on which this does not work has url request is made is like this:
http://hostname:8080/AreaName/Report/ViewReport
and u parameter of ajax is like /AreaName/MyAutoComplete/Search, so complete url to which autocomplete is made is like
http://hostname:8080/AreaName/MyAutoComplete/Search?callback=jQuery151013129048690121925_1327065146844
Server's response looks like this:
[{"Id":2,"Text":"001"},{"Id":7,"Text":"002"}]
I know it is not jsonp, for that it should be
<script>
jQuery151013129048690121925_1327065146844([{"Id":2,"Text":"001"},{"Id":7,"Text":"002"}]);
</script>
But I want to make normal json request, not jsonp.
UPDATE
Weirdest thing of all (I'm starting to think it is a bug in jQUery v1.5.1 which is used on project) is that when I remove dataType: "json", it makes a normal json request :)
So, instead of how to make json request, now I will accept an explanation to why this works as expected (and the one with dataType:"json" does not):
$.ajax({
url: u, type: "POST",
data: data,
success: function (d) { response($.map(d, function (o) { return { label: o.Text, value: o.Text, id: o.Id} })); }
});
From the bug here : http://bugs.jquery.com/ticket/8118
You are probably using jquery-validation plugin. Jquery-validation plugin is not compatible with jQuery 1.5 and the conflict causes the kind of issue you are having here.
If the problem is not specifically due to jquery-validation plugin, check if you have any other jquery plugin that might not be compatible with jQuery 1.5
I'm looking for a way to return a single JSON/JSONP string from a cross-domain "AJAX" request. Rather than request the string and have JQuery return it as a generic object automatically, I want to get a hold of the string BEFORE that conversion happens. The goal here is to parse it myself so I can turn it straight into new objects of a certain type (e.g. a Person object).
So, just to make this clear, I don't want any string-to-generic-object conversion going on behind the scenes and this must work using a different domain.
Here's a non-working example of what I would like to do:
$.ajax({
type: 'GET',
url: 'http://www.someOtherDomain.com/GetPerson',
dataType: 'text',
success: parseToPerson
});
function parseToPerson( textToParse ) {
// I think I can do this part, I just want to get it working up to this point
}
I'm perfectly happy if JQuery isn't involved in the solution, as long as it works. I would prefer to use JQuery, though. From what I've read, the javascript techniques used to get JSONP data (dynamically creating a script element) would probably work, but I can't seem to get that to work for me. I control the domain that I am requesting data from and I can get the data if I change the dataType in the AJAX call to 'JSONP', so I know that is working.
If your data is being retrieved from another domain, you will need to use JSONP (there are other options, but JSONP is by far the easiest if you control the service). The jQuery call will look like this:
$.ajax({
// type: 'GET', --> this is the default, you don't need this line
url: 'http://www.someOtherDomain.com/GetPerson',
dataType: 'jsonp',
success: parseToPerson
});
The actual request that goes to your service will be http://www.someOtherDomain.com/GetPerson?callback=arbitrary_function_name. On the service side, you will need to return data like this:
arbitrary_function_name("the string (or JSON data) that I want to return");
So you'll need to inspect the querystring parameters, get the value of the callback parameter, and echo it out as if you're calling a Javascript function with that name (which you are), passing in the value you want to provide through the service. Your success function will then get called with the data your service provided.
If you're deserializing the returned data into a Javascript object, you might be better off returning JSON data than a string, so the data your service returns might look like this:
arbitrary_function_name({
"name":"Bob Person",
"age":27,
"etc":"More data"
});
That way you don't have to worry about parsing the string - it'll already be in a Javascript object that's easy to use to initialize your object.
Not sure how this will work in conjuction with jsonp, but maybe converters is what you're looking for?
$.ajax(url, {
dataType: "person",
converters: {
"text person": function(textValue) {
return parseToPerson(textValue);
}
}
});
I frequently use this twitter api website to search. It provides its own api. Now I want to try it but do not know how to do it. My idea is:
Put a search box in html and in onclick event it will retrieve json
Parse that json and count the length of each tweet (only length of tweet text, not userid)
Display each tweet userid, date, link, text (all tweet data) in browser
Length of tweet text is appending to (3) for each tweet
I am new in javascript and json. Will you let me know how to retrieve json and do above?
First, it would help to review the fundamentals of JSON requests. Once you do that, feel free to brush up on jQuery and use jQuery.getJSON() to retrieve the JSON you want from a given URL. Here's some pseudo code:
$.ajax({
url: 'http://tweetscan.com/json.php',
dataType: 'json',
data: { 's' : 'some search phrase' },
success: function (response) {
// Do something with JSON response.
}
});