Skip to main content

Posts

Showing posts with the label ajax

jQuery ajax this.id undefined

I have a list of items I delete using AJAX. This list is a simple list with divs and each div as an id so when the item is removed from the database I return true and then it removes the line. Here my code: HTML <div id="row1"> <div>item1</div> <div><a href="...">view</a></div> <div><a id="1">delete</a></div> </div> JS $('.delete').click(function () { if (!confirm('Are you sure you want to delete?')) { return false; } $.ajax({ type: "POST", url: '/delete_record', data: 'id=' + this.id, cache: false, success: function (result) { if (result == 'good') { $('#row' + this.id).remove(); } } }); }); For some reason the this.id does not work because this.id is undefined ... why? I have id="1" on my a h

Prevent caching of AJAX call

It looks like that if I load dynamic content using $.get(), the result is cached in browser. Adding some random string in QueryString seem to solve this issue (I use new Date().toString()), but this feels like a hack. Is there any other way to achieve this? Or, if unique string is the only way to achieve this, any suggestions other than new Date() ? Source: Tips4all

Is this sufficient to protect against a CSRF for an ajax-driven application?

I'm working on a completely ajax-driven application where all requests pass through what basically amounts to a main controller which, at its bare bones, looks something like this: if(strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { fetch($page); } Is this generally sufficient to protect against cross-site request forgeries? It's rather inconvenient to have a rotating token when the entire page isn't refreshed with each request. I suppose I could pass and update unique token as a global javascript variable with every request -- but somehow that feels clumsy and seems inherently unsafe anyway. EDIT - Perhaps a static token, like the user's UUID, would be better than nothing? EDIT #2 - As The Rook pointed out, this might be a hair-splitting question. I've read speculation both ways and heard distant whispers about older versions of flash being exploitable for this kind of shenanigans. Since I know nothing about that

ASP.NET MVC and AJAX

I have a view which is composed of top, left and bottom headers and a main contents pane. Suppose that during an AJAX request I need to update the HTML of the top, bottom and main panels (the left header should stay the same). I was wondering what would be the best way to accomplish this. The first thought was to put the main contents panel into a partial and have a controller action that would return PartialView. This wouldn't work because as the action returns only the HTML of the main pane I cannot update the top and bottom headers. So if I put the top and bottom headers into their own respective partial views I would need my controller action to return multiple partial views. Is this possible at all or I am doing something completely off the track? I saw that it is possible to render a partial view to a string so I thought that I could use this technique in the action to return a JSON object with 3 properties representing the HTML of the 3 partials that I need to updat

jQuery / Ajax - $.ajax() Passing Parameters to Callback - Good Pattern to Use?

JavaScript code I'm starting with: function doSomething(url) { $.ajax({ type: "GET", url: url, dataType: "xml", success: rssToTarget }); } Pattern I would like to use: //where elem is the target that should receive new items via DOM (appendChild) function doSomething(url, elem) { $.ajax({ type: "GET", url: url, dataType: "xml", success: rssToTarget(elem) }); } I don't think I can get the callback to work this way, right? What is the proper pattern? I don't want to use global variables necessarily to temporarily hold the elem or elem name. Source: Tips4all

Cannot properly set the Accept HTTP header with jQuery

I'm trying to set the Accept HTTP header to "text/xml" with this jquery code: $.ajax({ beforeSend: function(req) { req.setRequestHeader("Accept", "text/xml"); }, type: "GET", url: "[proper url]", contentType: "text/plain; charset=utf-8", dataType: ($.browser.msie) ? "text" : "xml", username: '---', password: '-------', success: function(data) { var xml; if (typeof data == "string") { alert("Data is string:" + data); xml = new ActiveXObject("Microsoft.XMLDOM"); xml.async = false; xml.loadXML(data); } else { xml = data; alert("Data is not string:" + $(xml).text()); } // Returned data available in object "xml" //alert("Status is: " + xml.

Update whole page on Ajax request

I have an AJAX request that can have two possible outcomes: The server responds with a message which I should place in a <div> The server responds with an HTML page, in this case I need to substitute current page with a new one and change the address (the client knows the address before a request). What would be the solution if I have the AJAX request that needs to handle both of these cases? url = "http://example.com" ajax.request(callback) function callback(response) { if (case2(response)) { history.pushState({}, "New page", url); document.innerHTML = response } else { updateDiv(response) } } I'm interested in a correct way to implement the first branch, or if the server can somehow compose a headers that will make browser to handle a response as a usual HTTP response and update a page location and content, something like redirect with given content. I understand that the server can retu

Iphone runs ONLY from cache even when online

I am having a bit of trouble. I am making a mobile version of our website, and want to cache some of the more important pages, not just for use offline, but for added speed in some cases. First question: Do I have to manually cache the pages to speed up performance? I am using AJAX calls to bring in content, and it obviously doesn't work offline without manually caching, but while online is Safari going to request a page via ajax if it's already in the cache? I currently have it working correctly. My pages cache and work both online and offline. But if I don't specify all pages I want cached, it appears with every ajax call, those pages are requested again, not loaded from cache. Second Question (My actual problem): When online or offline, and using a .manifest file to manually cache files, it appears that the iPhone sets itself in a "offline" mode, even when online. So when I run my ajax call, if the page is not cached, I get an error. $.ajax({ url: p

Multiple jqGrids fires ajax call for every jqGrid on page

I have three grids each has a unique: pager, ID, and wrapper. These grids are hidden by default and show upon selection of a dropdown box. At page load, before anything is selected, jqgrid makes three ajax calls for its data, but after debugging it appears that each grid makes three calls, one for each of the grids on the page. I would like each grid to only request its data for its own grid. Typically the first grid times out and the other two work flawlessly. I believe that the first one may bog down the number of connections to the server causing it to timeout and then the others are then free to proceed because they are below the max number of connections. Any thoughts, ideas, or suggestion are greatly appreciated.

Closing the jsp page based on user choice

I have a jsp page where if the user closes the page the popup box should appear asking whether he really wants to close the page. If he chooses to close the page then i need to redirect to another jsp page using ajax. How do i achieve the same. I have a sample code which asks the user when he clicks the close button but this message is getting popped up when refresh,submit or clear buttons or any other buttons are pressed. Can anyone sggest to modify the given code or any new functionality which achieves the same. sample.jsp <html> <head> <title>Refresh a page in jQuery</title> <script type="text/javascript" src="jquery-1.4.2.min.js"></script> </head> <body> <h1>Stop a page from exit with jQuery</h1> <button id="reload">Refresh a Page in jQuery</button> <script type="text/javascript"> $('#reload').click(function() { location.reload(); }

How to assign value to $_SESSION with AJAX?

Yes, I'm aware that this question has already been post, but ... I'm looking for a way to check if the Client has Javascript Enabled. Here is my idea : At every loading page, PHP initialize $_SESSION['is_js'] to 0 . Then, I wish AJAX to ( via launching a script php ? ) try to set $_SESSION['is_js'] to 1 . If JS is enabled, AJAX will succeed, if not, the value remains 0. I can't use jQuery or others libraries... I have to write this in pure AJAX ( I mean not using framework or librairies ), and I have no idea how to do this. Edit : I can't use cookie. I don't want to use <noscript> .

status box like twitter status box [closed]

I am a new PHP learner. I need a status box which just like twitter status box. I have tried many websites and i can not find the exact solution. What i exactly need is 1. Status box like twitter, when typing on the box it will increase its size automatically 2.If there is URL in the status, it should automatically highlight URL like twitter 3.After 500 characters user should not allowed to type in. I don't want to use any API. I think the code might be combination of HTML,ajax and javascript. Please someone help me. I am new in this area. So if it is possible give a little explanation of your code. I found a bit similar question in this website but the answer is not given there.

Ajax call response in struts. Response not reflected in jsp.

I am calling a struts action on using an ajax call. In it i am changing the values of some properties of the action class. The values are set properly but i am not able to reflect the changes in the jsp. The jsp code is as follows. <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> function findindex1() { var temp=document.getElementById("country").value; if(window.ActiveXObject){ request = new ActiveXObject("Microsoft.XMLHTTP"); } else if(window.XMLHttpRequest){ request = new XMLHttpRequest(); } request.onreadystatechange = showResult; request.open("POST",'temp.action?index='+temp,true); request.

How do you dynamically set the upload target path when using an AJAX based file uploader?

I'm trying to implement Valum's file uploader (improved by Ben Colon https://github.com/bencolon/file-uploader ) and I'm running into a problem. Previously, my upload.php had the upload target set in accordance to certain input fields. See here: //Directory where files are stored if ($grade == '9') { $target = "storage/g9/"; } elseif ($grade == '10') { $target = "storage/g10/"; } elseif ($grade == '11') { $target = "storage/g11/"; } elseif ($grade == '12') { $target = "storage/g12/"; } $target = $target . $_POST['snumber'] . "." . $extension; move_uploaded_file($_FILES['upload']['tmp_name'], $target); Now, with an AJAX based file uploader it starts uploading before you even hit submit so PHP doesn't get a chance to pickup the input fields to decide what the upload path is going to be, it can only go to a predefined one. To see how the AJAX file uploader does

jQuery doesn"t .load() until .ajax() within callback of .load() is complete?

My problem is that the <div> with the jQuery progress bar ( dnc_scrubber.html ) isn't loaded until all of the other requests are complete. Is there anyway around this? I want to change the content of #tabs-1 to the progress bar, and animate it while dnc_scrubber.php is doing its work. The lines.php and progress.php files are used to calculate the percentage of work done by dnc_scrubber.php - which updates the session as it goes along. lines.php and files.php return the session variable. $('#tabs-1').load('dnc_scrubber.html', function() { var querystring = 'col=' + col + '&' + makeQS(files); var lines = 0; $('#progressbar').progressbar(); $.ajax({ url: 'dnc_scrubber.php', type: 'POST', async: true, data: querystring, complete: function() { for (i = 0; i < files.length; i++) { $('#complete').append('<a h

cross origin domain

I am trying to access a PHP file which is located in my domain but I want to do that using AJAX with jQuer. I am using a localhost and from there I am trying to call the PHP file here is the code I am using : <script> $(function() { $("#callAjax").click(function() { var theName = $.trim($("#theName").val()); if(theName.length > 0) { $.ajax({ type: "POST", url: "http://studiofutbol.com.ec/upload_file.php", data: ({name: theName}), cache: false, dataType: "text", success: onSuccess }); } }); $("#resultLog").ajaxError(function(event, request, settings, exception) { $("#resultLog").html("Error Calling: " + settings.url + "<br />HTPP Code: " + request.status

How do I resend a failed ajax request?

I have multiple ajax requests some request data every minute others are initiated by the user through a ui. $.get('/myurl', data).done(function( data ){ // do stuff.. }); The request might fail due to an authentication failure. I've setup a global .ajaxError() method for catching any failed requests. $(document).ajaxError(function( e, jqxhr ){ // Correct error.. }); After I catch the error I reset authorization. Resetting the authorization works but the user has to manually re initiate the ajax call (through the ui). How do I resend the failed request using the jqxhr originally sent? (I'm using jQuery for the ajax)

JQuery Ajax - Code did work now it does not?

I built a basic form with ajax validation and page redirection with PHP and jQuery. This was working fine a few days back. I have come back to it and the ajax seems to have stopped working. At the moment the ajax is just checking a test var and echoing back data. It is really bugging me as it working fine initially! Seems strange as I cannot spot anything obvious, hoping some fresh eyes can help. The ajax has stopped sending data across so I click submit and the ajax sends nothing so my if() does not do anything! I get the form values fine. Tried with basic ajax and alerts, something strange seems to be happening. Hope you can help <form id="registerform"> <ul id="form"> <li> <label for="email">Email:</label><span class="fieldbox"><input type="text" id="email"/></span> </li> <li> <label for="confirmemail&qu