I use Google Custom Search on my website (JSON API, not generated script tag from Google). On search form submit I send ajax request to https://www.googleapis.com/customsearch/v1?q=QUERY&cx=MY_ENGINE&key=MY_API_KEY
The result is JSON object with all those informations about search results. As it is async, I don't have to refresh page. I just fill
<div id="searchResult></div>
with data I have received.
But how can I handle JSON response on amp page (without refreshing)?
Use amp-list component in combination with amp-mustache
Related
Can you send a user to the url of a http post as if the user had clicked the submit button of a form.
Expected result is that the page does not populate a form with values that can be seen in page source.
I am using Classic ASP. This is the http post which posts the data but does not send the user on like it would if it was a normal form submission:
<%
strData = "some-key-value-pairs"
set objHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
objHttp.open "POST", "https://www.some-domain.com", false
objHttp.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
objHttp.Send strData %>
I want the above to 'go to' the URL sending along the post data. Is this possible?
EDIT: the page is posting to an external domain. Data must be sent using the above method but want to know if this can be done without actually using a form which is populated client side. I dont want the form values to be readable in page source but i still need the page to continue to the url the "form values" were posted to - including the post values so that the external page receives the data just like a form post to then be able to display the result to the user.
You could create a hidden form dynamically with javascript in the browser. As part of that routine, you could make an ajax call to get the form values. Then when the form values are retrieved, submit the form to the external page, which would display the result to the user. With this method the user could still see the form values if they watch the ajax calls, but they wouldn't be there in the view source.
you should be able to use Server.Transfer for this, assuming you don't mind if the url in the address remains the same.
http://www.w3schools.com/asp/met_transfer.asp
otherwise, you would need to do this with client side javascript, but I don't think you're looking to go that route.
I want to load some form elements for a custom post type's edit screen via AJAX.
What's the best practice to implement this?
Should I use this algorithm?
Hook an action to admin_menu to
detect the request from AJAX client (via x-requested-with),
clean the buffer (via ob_clean),
output the requested element, and exit.
Request wp-admin/post-new.php from AJAX client
I have a mobile site which completely runs using AJAX, and hash code, basically each page click is a link, such as
<a href='http://some-domain.com/my-page-122.php" hash-id='122'>linkage</a>
Meaning that the page itself exists and it has ON IT google analytics page, HOWEVER, on the ajax request, I only ask to load a certein <div> on said page using jQuery's load(), so my question is:
because the page is called for in it's entirety with the google analytics code and everything, will it still record it as a page view even though only a portion is injected to the page?
The reason why I'm asking is because this site is getting around 500 uniques per day, and we want to change it to this new AJAXy form, so not recording analytics is a big no-no.
If you use jQuery you can bind to the global AjaxComplete event to fire a Pageview everytime an Ajax call completes:
jQuery(document).ajaxComplete(function(e, xhr, settings){
var d = document.location.pathname + document.location.search + document.location.hash;
_gaq.push(['_trackPageview', d]);
});
If you update the Anchor every time you do an Ajax call this will fire the full path including the anchor part of the url.
Note that if you load content using .load that has the Google Analytics Tracking code in it, it will run that code and fire a second pageview. So you want to make sure you don;t include the GATC on the ajax content to avoid double pageviews.
Analytics won't record it automatically. Assuming you're using the asynchronous code you can record as many pageviews as you want by writing to the gaq array using an explicitly set URL:
_gaq.push(['_setAccount', 'UA-12345-1']);
_gaq.push(['_trackPageview', '/home/landingPage']);
In this case you can build whatever URL you want where they have '/home/landingPage'. Note that if _gaq was already properly instantiated and _setAccount was already pushed then you only need to push the _trackPageview.
Also, the event can be in code returned by your AJAX, or it can be in the click event of your button or whatever is launching the AJAX request.
See http://code.google.com/apis/analytics/docs/gaJS/gaJSApiBasicConfiguration.html#_gat.GA_Tracker_._trackPageview
I have a button in JSP, when clicked, goes to a servlet stores a java object using request.setAttribute("attr", object) and forwards to another page. In that page, I am using a custom JSP tag which gets this attribute and displays some values. Now I want this all to happen using AJAX. I want to have only one page which submits a form and receives an object to be used by custom JSP tag in the same page. How do I do this ? Is there a reliable library for that ?
From what I see, in ajax I can send response by printing it which means I must send an XML back. If I do so, how do I convert it back to java object so that JSP tag can use it ?
Assuming your custom tag just displays some data, you could submit your form via ajax and return HTML. Then just push that HTML into a div. The HTML that is returned would be what your JSP with the custom tag renders, jQuery can help you out...
pseudo code:
$.post(url, params, function(htmlData) {
$('#results').html(htmlData);
});
On the server side, nothing would really change from the way you are handling it now. If you don't need to post a form but just submit some data via ajax, you could also use the load() function.
Your ajax request will only return XML if you return XML. The response type is entirely up to you.
When we use AJAX for validation the response comes through xml file and in case of javascript the response comes through html page.
Is it true or false ?
please explian both the cases.
Ajax can return pretty much anything. Your particular application or implementation might be returning XML, but Ajax commonly returns JSON and HTML as well. But it can return TXT or just status codes ..
AJAX is such a broad term that both of your cases can be called like that. Basically every call from already loaded page is AJAX even if it's done by dynamically creating tag or refreshing iframe.
actually Ajax is nothing but a term to describe what is happening, the response comes as an xml or html depending on what you want it to return, it is a string that ajax returns, in Javascript it is up to you what to do with that string, want to parse it as XML node? want to display it immidiately in an element "innerHMTL" or want to alert the returned string... the power of ajax is not what it returns, it is what it does on the server (remotly) without having to send the whole page in.
AJAX there can be any type of return text. With Javascript it can work on the DOM object and get anything from the page.
The main difference, AJAX a request goes to the server and the result is returned.