I would really appreciate If anyone can explain methe following:
document.getElementById("txtHint2").innerHTML=xmlHttp.responseText;
I understand that result (right side after equal sign) will be written within div tag in this case...But what is this RESPONSETEXT actually?
I have html calling some *.js. In this I have url to php. In php is command connect to database and give results after my query.
So result of this query sholud be related to =responseText ...
I am confused and not so familiar about this ...Please help me!!!
you may read;
Using jQuery and JSON with AJAX responseText?
It contains the content from the URL you requested using the XMLHttpRequest object.
The object xmlHttp has the property responseText that is the response of your XML HTTP Request ("ajax" return). It's like when you load a page in a browser and you get your page.
This object has many properties, like:
responseText: Returns the response data as a string
responseXML: Returns the response data as XML data
Referece: http://www.w3schools.com/dom/default.asp.
xmlHttp.responseText is the body of the response from the server. It is whatever was sent back from the server based on the request (specified in the xmlHttp object). It could be a simple string of text, or a snippet of HTML, or JSON to be parsed and used by your code.
the requested url will generate an output(text/html). This output is sent to the client(requested page) through xmlHttp.responseText;
You may also get xml as an output, then you need to use xmlHttp.responseXML; you need to parse this and use it in your application..
Related
I am using this code to share on linkedin.
IN.UI.Share().params({
url: "http://www.example.com"
}).place();
It's working perfect, but I need to get response after sharing the post.
Thanks in advance.
you may want to simply do an AJAX query to the REST api. you'll get back a 201 response with some XML data. Per their website:
Response
Returns 201 Created on success. Upon success, the body of the response will contain the following:
<update>
<update-key>UNIU-8219502-5705061301949063168-SHARE</update-key>
<update-url>http://www.linkedin.com/updates?discuss=&scope=8219502&stype=M&topic=5705061301949063168&type=U&a=aovi</update-url>
</update>
You can use the update key to request the XML or JSON representation of the newly created share. This can be achieved by making a GET call to http://www.linkedin-ei.com/v1/people/~/network/updates/key={update_key} (setting {update_key} to the value you received in the previous response)
I am trying to implement a simple request to Wikipedia's API using AJAX (XMLHttpRequest). If I type the url in the address bar of Firefox, I get a neat XML, no sweat there. Yet, calling the exact same url with:
// this is my XMLHttpRequest object
httpObjectMain.open("GET", "http://en.wikipedia.org/w/api.php?action=query&format=xml&prop=langlinks&lllimit=500&titles=kaas", true);
httpObjectMain.send(null);
returns an empty response. According to FireBug, I get a 200 OK response, but the content is just empty.
I suspect I might be missing something on the header of the GET http request.
Help! (and thanks!)
The Wikipedia API does support JSONP.
Your query string'll become something like this:
http://en.wikipedia.org/w/api.php?action=query&format=json&callback=test&prop=langlinks&lllimit=500&titles=kaas
But you'll have to build the jsonp handler (or you can use your favorite library to do it), switch to json output format from the xml you choose and create the callback function to parse the result and do the stuff you need on the page.
The browser will not allow you to send an XHR to another domain other than the one the page is on. This is for security purposes.
One way around this that I have seen is to setup a proxy on the domain the page is hosted on that will pass requests through to the actual api server. See http://ajaxpatterns.org/Cross-Domain_Proxy
i have a JSON object in server side:
String jsonText = JSONValue.toJSONString(users);
and i want to send it to client side to be used in a javascript function, how to do that ?
I am using JSF 2 with Pure JavaScript with no other libraries.
In HTTP, the server doesnt send anything to client-side. The client-side asks for some resource using a request, and the response contains the resource.
Make the JavaScript function send an AJAX request to the server, and make the server answer with this JSON string in the response (by writing the JSON String to the response writer).
See http://api.jquery.com/jQuery.getJSON/ to get a JSON String from JavaScript. The server code is as simple as
response.getWriter().print(jsonText);
The easiest way is to let JSF print the string representation of the JSON object as a JS variable in the same view:
<script>var users = #{bean.usersAsJson};</script>
The correct way, however, is to use a fullworthy JSON web service for this. JSF is a component based MVC framework, not a JSON web service. For that the Java EE stack offers the JAX-RS API. Long answer short: Servlet vs RESTful. Use this preferably in combination with jQuery or whatever JS framework which is able to send Ajax requests and manipulate the HTML DOM tree in basically an oneliner. You only need to take into account that when used in combination with JSF, this can be used for pure presentation only.
The browser must request it, ie that string must sit somewhere in a request handling chain; set the response to that string and the browser will receive it.
I think you wanted to say response the json string.
It can be done by jQuery function getJSON.
It will get the json string and parse it inti hash, then you have to process it for your needs.
API documentation
send your json object to response and get the response in the javascript.
use eval function to evaluate json object.
var jsonObject = eval('(' + request.responseText + ')');
I have some problem with $.getJSON response in Chrome
The query is
$.getJSON("http://www.askgeo.com/api/428014/sf2t36ujv1tsf325t5734gstr4/timezone.json?callback=?&points=55.77184,37.623553",
function(json){
<some code>
}
);
if you click on this link you'll get an json text.
By when I run this query Chrome shows an error:
Resource interpreted as Script but transferred with MIME type application/json
SyntaxError: Unexpected token : timezone.json:1
Does it try to convert json response to JavaScript object? If it is so why it cann't do that? Is there any way of resolving this problem?
in Chrome debugger I found the file "timezone.json" with this content:
{"code":0,"message":"ok","data":[{"timeZone":"Europe/Moscow","currentOffsetMs":14400000,"latitude":55.77184,"longitude":37.623553}]}
The server you are requesting data from is not setup to return JSONP. therefore, you need to build some kind of proxy to get the data for you, or use YQL.
Edit:
If you were to use YQL, this is the url you would use:
http://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20json%20WHERE%20url%3D%22http%3A%2F%2Fwww.askgeo.com%2Fapi%2F428014%2Fsf2t36ujv1tsf325t5734gstr4%2Ftimezone.json%3Fpoints%3D55.77184%2C37.623553%22&format=json&diagnostics=true
and for information on how I generated that url, visit:
http://developer.yahoo.com/yql/console/#h=SELECT%20*%20FROM%20json%20WHERE%20url%3D%22http%3A//www.askgeo.com/api/428014/sf2t36ujv1tsf325t5734gstr4/timezone.json%3Fpoints%3D55.77184%2C37.623553%22
You can find the url at the bottom.
Fiddle using YQL: http://jsfiddle.net/JGwU3/1/
there is however one quirk with using YQL. if the result only contains one result, it's contents is an object, however, if it is multiple, its contents will be an array. you can see the difference by console.logging the response.
In the API documentation it says, that you should provide the query paramaters in a separate JSON object as the second argument.
$.getJSON('http://www.askgeo.com/api/428014/sf2t36ujv1tsf325t5734gstr4/timezone.json', {'callback':'', 'points': '55.77184,37.623553'}, function(json) {
alert(json.data[0].timeZone);
});
Works fine when I tested it.
(This is totally ignoring JSONP)
EDIT
OK, my post was wrong, it is a JSONP issue. See this jQuery documentation page on how to retrieve the data from the URL:
http://api.jquery.com/jQuery.ajax/
I'm looking to write some Javascript which will make an Ajax PUT or POST request to an HTTP server. I'm assuming that the information which gets passed as the argument to request.send needs to be in XML format. Could somebody shoe me an example of how to create this XML and pass it to request.send([Entity-body]) as the entity-body.
Thanks!
The "xml" in XmlHttpRequest is entirely superfluous; there's never been a requirement that the request or the response is in xml format.
In fact, it's just as common to send JSON (JavaScript object notation) instead of xml.
So, don't use xml unless you want to. Just send any string you want.
You don't have to send xml, you can send any arbitrary string.
an example for http post would be
request.send("id=1&somattribute=value&etc=etcetc");
where you have name value pairs
name=value
separated by &