Javascript jquery to contact an API - javascript

I'm trying to connect to a webapi at a location that looks like this from inside my js jquery file.
example.com/?var=input
Is there a simpler way to do it than an ajax call?

I would use AJAX for this, but I guess you could open a hidden IFRAME with the URL set to the page you want to connect to (not sure why you would do this though!).
Maybe use a JavaScript library like JQuery to make life easier?
If the data you're trying to access is returned as JSON then you can get around the browser security problems.
Here is a JQuery example where a request is made to Flickr.com from JQuery.com:
docs.jquery.com/Ajax/jQuery.getJSON

You may be run into cross domain issues if you do it with an ajax call.
Call the web-api from serverside, it would be the most appropriate way.

Related

can AJAX do anything else than load a JSON file?

I'm want to (or think I need to) use AJAX to accomplish what I intend.
When clicking on a specific link in a list of links, I want to fill the HTML markup below with content of specific subpages. The data is naturally somewhere in the database and actually easily accessible with the CMS's API (I'm using Processwire).
I'm quite new to coding and especially AJAX and all documentation I find online only mention it in combination with a JSON file that would be loaded via AJAX.
However, I don't have a JSON file on the server, that means, according to my understanding, I would need to
store the data I need in a multidimensional php array,
use json_decode to create and then save that JSON-file on the server,
load that file via AJAX and process through more JS.
Let alone keep that JSON-file updated (or create a new one and delete the old one?) since new content will arrive periodically. It seems unnecessarily complicated to me, but what do I know.
There's got to be a better way…
Any help is appreciated.
AJAX is simply a way to make a request to the web server for information.
When you make an AJAX request you ask for a response from a file on a server. So, you can send an AJAX request to a PHP script for-instance.
The PHP script could return anything, JSON is common and very widely used response format, but XML might be another one you've encountered.
So, your request for information is made using AJAX, and the response you get back is JSON.
You don't need to store a JSON file on your server. You just need to make an AJAX request that returns current data in JSON format.
AJAX allows you to do asynchronous HTTP requests.
You can of course ask for a json file, but you can also (for example) call an API.
I suggest you start by reading the the getting started guide for AJAX in MDN:
https://developer.mozilla.org/en-US/docs/Web/Guide/AJAX/Getting_Started

Appending my jsession ID at every ajax call by Jquery

I am running my web application on Weblogic
application server.I am making several ajax calls in my application via Jquery UI $ajax and other Jquery Plugins (i.e. Jquery Datatables and JSTree)
Now i need to maintain same session in all these calls.Any user in the same session should only be allowed to do this.For this i am trying to append the JSESSIONID to every ajax call.
The problem with this approach is that i don't know how to get this value in my javascript file.If i write my code in JSP ,i can do that but in javascript i do not know of a way to do this.
Please help me with a way to set it into Jquery so that it gets passed with every request.
Thanks
The best solution is to put it in a session cookie, which will be passed with the AJAX requests and can be interpreted by the server. No changes needed to any JavaScript AJAX requests.
The next would be more complicated. You could write a jQuery.ajaxSend() function that checks the request url (to make sure it's one you need to add the session ID to) and add the sessionID to the data object before the AJAX request is made.

Internet explorer intercepts XML response

I have a form whose target is an iframe.
When submitting the form, the response is XML and I have Javascript that analyzes the response.
I noticed that when running on IE, IE intercepts the response and treats it as an RSS feed, so my code never receives the response. If I disable the RSS feeds (from the internet option, content tab) everything works ok.
I set the content type of the response to “text/xml; charset=UTF-8” but still it does not work.
Is there any workaround?
The best workaround would be not to use an iframe in this case. It sounds like IE is catching the http response and reading it on its own. Is there a reason you're not making an AJAX call to retrieve the information? It sounds like you're relying on JavaScript to handle the response anyway, so I would think that using an XMLHttpRequest object would be better for you: http://www.w3.org/TR/XMLHttpRequest/
If that's too complicated, look into a library like jQuery: http://jquery.com/ that has built in (and much simpler) functions to make AJAX calls and handle responses.
To expand on this, you would bind the submit function of the form to a JS function (or use jQuery to do it) and pick up the form data, send it in an AJAX request, and handle the response. jQuery has a built in function serialize() which is meant to convert form data on a page into information ready for use in the ajax() function to send to the server. If you're unfamiliar with the XMLHttpRequest object, I would highly suggest using a library like jQuery for this task.
OK, found the problem…
My response XML contains FEEDBACK tags.
IE treat these tags as RSS feeds. Changing the tag name to FDBACK resolve this issue…
MS, why this is not documented???
Yes, also make sure the file is output with the correct Content-Disposition using headers, do that IE gets "response.xml" and not "response.php" or some such...
'Content-Disposition: attachment; filename="response.xml"'

Reading in a text file in Javascript or Jquery

Im new to Jquery and Javascript and im wondering how can i read information from a text file stored on my server and write back to this file again afterwards? I couldn't find much information after googling the problem
I would also like to read in the data line by line and store it in an array, is this possible?
Thanks,
Matt
I think you want to use some server side programming. If you want the data on the client, you can use javascript or jquery using some ajax solution.
You can read the file if server allows to get it via http but you cannot write to it
Read file can be done by simply $().load(url)
You could use a RESTful API in order to achieve this. Use HTTP GET (jQuery/AJAX) to get the contents of a file, edit what you wish, and use a HTTP PUT (jQuery/AJAX) to write the changes back to the server.
Read information - yes via ajax call. The javascript will get the text from the page - whatever it is html/php/asp and return it to the calling page.
Read line by line - it is possible if you write it yourself. There is no built-in function for this. But then the text should be divided by lines.
Write back - JavaScript can`t do it by itself. You need an ajax and serverside.

Cross domain Ajax request from within js file

Here's the problem:
1.) We have page here... www.blah.com/mypage.html
2.) That page requests a js file www.foo.com like this...
<script type="text/javascript" src="http://www.foo.com/jsfile.js" />
3.) "jsfile.js" uses Prototype to make an Ajax request back to www.foo.com.
4.) The ajax request calls www.foo.com/blah.html. The callback function gets the html response and throws it into a div.
This doesn't seem to work though, I guess it is XSS. Is that correct?
If so, how can I solve this problem? Is there any other way to get my html from www.foo.com to www.blah.com on the client without using an iframe?
It is XSS and it is forbidden. You should really not do things that way.
If you really need to, make your AJAX code call the local code (PHP, ASP, whatever) on blah.com and make it behave like client and fetch whatever you need from foo.com and return that back to the client. If you use PHP, you can do this with fopen('www.foo.com/blah.html', 'r') and then reading the contents as if it was a regular file.
Of course, allow_remote_url_fopen (or whatever it is called exactly) needs to be enabled in your php.ini.
There is a w3c proposal for allowing sites to specify other sites which are allowed to make cross site queries to them. (Wikipedia might want to allow all request for articles, say, but google mail wouldn't want to allow requests - since this might allow any website open when you are logged into google mail to read your mail).
This might be available at some point in the future.
As mentioned above JSONP is a way around this. However, the site that you are requesting the data from needs to support JSONP in order for you to use on the client. (JSONP essentially injects a script tag into the page, and provides a callback function that should be called with the results)
If the site you are making a request to does not support JSONP you will have to proxy the request on your server. As mentioned above you can do this on your own server or what I have done in the past is use a http://www.jsonpit.com, which will proxy the request for you.
One option is to implement a proxy page which takes the needed url as a parameter. e.g. http://blah.com/proxy?uri=http://foo.com/actualRequest
JSONP was partially designed to get around the problem you are having
http://ajaxian.com/archives/jsonp-json-with-padding
JQuery has it in their $.getJSON method
http://docs.jquery.com/Ajax/jQuery.getJSON
The method shown above could become a large security hole.
Suggest you verify the site name against a white list and build the actual URI being proxied on the server side.
For cross domain hits this is a good working example and now is considered as some what "standard" http://www.xml.com/pub/a/2005/12/21/json-dynamic-script-tag.html.
there are other ways as well, for eg injecting iframes with document.domain altered
http://fettig.net/weblog/2005/11/28/how-to-make-xmlhttprequest-connections-to-another-server-in-your-domain/
I still agre that the easy way is calling a proxy in same domain but then it's not truly client side WS call.

Categories

Resources