Posts

Showing posts with the label Ajax

Ajax - JSON Doesnt Get Sent In PATCH Only

Answer : First, check that you use latest version of jQuery library: Older versions directly restrict unknown methods (PATCH is new one). I've tested on jQuery 1.7 - PATCH method working without problems. Second, not all browsers supports PATCH method using XMLHttpRequest: Like, IE 7,8 (9+ works okay) have XMLHttpRequest, but it throws an error on PATCH: new XMLHttpRequest().open('PATCH', '/'); //Illegal argument To fix this, you may force jQuery to use the old proprietary ActiveXObject xhr, like so: $.ajax({ url : 'http://127.0.0.1:8001/api/v1/pulse/7/', data : data, type : 'PATCH', contentType : 'application/json', xhr: function() { return window.XMLHttpRequest == null || new window.XMLHttpRequest().addEventListener == null ? new window.ActiveXObject("Microsoft.XMLHTTP") : $.ajaxSettings.xhr(); } }); A bit late, but this worked for me when I got...

Caching A Jquery Ajax Response In Javascript/browser

Answer : cache:true only works with GET and HEAD request. You could roll your own solution as you said with something along these lines : var localCache = { data: {}, remove: function (url) { delete localCache.data[url]; }, exist: function (url) { return localCache.data.hasOwnProperty(url) && localCache.data[url] !== null; }, get: function (url) { console.log('Getting in cache for url' + url); return localCache.data[url]; }, set: function (url, cachedData, callback) { localCache.remove(url); localCache.data[url] = cachedData; if ($.isFunction(callback)) callback(cachedData); } }; $(function () { var url = '/echo/jsonp/'; $('#ajaxButton').click(function (e) { $.ajax({ url: url, data: { test: 'value' }, cache: true, beforeSend: function () { i...

Ajax File Upload With Form Data Laravel 5.3

Answer : Try using the FormData in ajax while you upload a file. Just try this $('form').submit(function(event) { event.preventDefault(); var formData = new FormData($(this)[0]); $.ajax({ url: '{{ url('/agents') }}', type: 'POST', data: formData, success: function(result) { location.reload(); }, error: function(data) { console.log(data); } }); }); OR You can try with this jQuery library https://github.com/malsup/form EDIT public function store(Request $request) { if (User::where('phone_number', '=', Input::get('phone_number'))->exists()) { return $this->respondBadRequest('Phone Number Exists'); } else { $user=User::create($request->all()); if($request->hasFile('image')) { $file = $request->file('i...

Codeigniter CSRF Valid For Only One Time Ajax Request

Answer : In my opinion you should try to recreate your csrf token each request Try this code example... For the js funcion var csrfName = '<?php echo $this->security->get_csrf_token_name(); ?>', csrfHash = '<?php echo $this->security->get_csrf_hash(); ?>'; ("#avatar").change(function(){ var link = $("#avatar").val(); var dataJson = { [csrfName]: csrfHash, id: "hello", link: link }; $.ajax({ url : "<?php echo base_url('main/test'); ?>", type: 'post', data: dataJson, success : function(data) { csrfName = data.csrfName; csrfHash = data.csrfHash; alert(data.message); } }); }); and for the controller public function test() { $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $confi...

Access Control Request Headers, Is Added To Header In AJAX Request With JQuery

Answer : Here is an example how to set a request header in a jQuery Ajax call: $.ajax({ type: "POST", beforeSend: function(request) { request.setRequestHeader("Authority", authorizationToken); }, url: "entities", data: "json=" + escape(JSON.stringify(createRequestObject)), processData: false, success: function(msg) { $("#results").append("The result =" + StringifyPretty(msg)); } }); This code below works for me. I always use only single quotes, and it works fine. I suggest you should use only single quotes or only double quotes, but not mixed up. $.ajax({ url: 'YourRestEndPoint', headers: { 'Authorization':'Basic xxxxxxxxxxxxx', 'X-CSRF-TOKEN':'xxxxxxxxxxxxxxxxxxxx', 'Content-Type':'application/json' }, method: 'POST', dataType: 'json', data: YourData, success: function(da...

Add Onclick Event To SVG Element

Answer : It appears that all of the JavaScript must be included inside of the SVG for it to run. I was unable to reference any external function, or libraries. This meant that your code was breaking at svgstyle = svgobj.getStyle(); This will do what you are attempting. <?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg xmlns='http://www.w3.org/2000/svg' version='1.1' height='600' width='820'> <script type="text/ecmascript"><![CDATA[ function changerect(evt) { var svgobj=evt.target; svgobj.style.opacity= 0.3; svgobj.setAttribute ('x', 300); } ]]> </script> <rect onclick='changerect(evt)' style='fill:blue;opacity:1' x='10' y='30' width='100'height='100' /> </sv...

Call Custom Confirm Dialog In Ajax.Beginform In MVC3

Answer : I came across this to customize AjaxOptions Confirm text with a value that has not been created yet when Ajax.Beginform is rendered . For example: Confirm="Are you sure you want to create Customer Id" + someValue + "?" Finally a found a solution: The approach is regarding a change in submit button behavior with JQuery to pull the value, run my own Confirm dialog and submit the Ajax form if user confirm. The steps: 1- Remove Confirm from AjaxOptions and avoid set button's type="submit", could be type="button" <div> @using (Ajax.BeginForm("Function", "Controller", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "theForm", InsertionMode = InsertionMode.Replace, LoadingElementId = "iconGif", OnBegin = "OnBegin", OnFailure = "OnFailure", OnSuccess =...

Add A Quantity Field To Ajax Add To Cart Button On WooCommerce Shop Page

Answer : Updated on May 2020 For WooCommerce versions from 3.2 to 4+, Optimized jQuery code and Removed a quantity bug. The following custom function is hooked in woocommerce_loop_add_to_cart_link filter hook and adds a quantity input field to each product on WooCommerce archives pages and other product loops. We use here mostly the original WooCommerce code. A bit of jQuery code is necessary to update the data-quantity attribute on the add to cart button when customer changes the quantity. Some styling might be needed, depending on your client wishes (and on your theme) . An additional section to hide the "View cart" button is located at the end. The code: add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_loop_ajax_add_to_cart', 10, 2 ); function quantity_inputs_for_loop_ajax_add_to_cart( $html, $product ) { if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() &...

Can Scrapy Be Used To Scrape Dynamic Content From Websites That Are Using AJAX?

Image
Answer : Here is a simple example of scrapy with an AJAX request. Let see the site rubin-kazan.ru. All messages are loaded with an AJAX request. My goal is to fetch these messages with all their attributes (author, date, ...): When I analyze the source code of the page I can't see all these messages because the web page uses AJAX technology. But I can with Firebug from Mozilla Firefox (or an equivalent tool in other browsers) to analyze the HTTP request that generate the messages on the web page: It doesn't reload the whole page but only the parts of the page that contain messages. For this purpose I click an arbitrary number of page on the bottom: And I observe the HTTP request that is responsible for message body: After finish, I analyze the headers of the request (I must quote that this URL I'll extract from source page from var section, see the code below): And the form data content of the request (the HTTP method is "Post"): And...

Wordpress - Ajaxurl Not Defined On Front End

Answer : In backend there is global ajaxurl variable defined by WordPress itself. This variable is not created by WP in frontend. It means that if you want to use AJAX calls in frontend, then you have to define such variable by yourself. Good way to do this is to use wp_localize_script . Let's assume your AJAX calls are in my-ajax-script.js file, then add wp_localize_script for this JS file like so: function my_enqueue() { wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/my-ajax-script.js', array('jquery') ); wp_localize_script( 'ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) ); } add_action( 'wp_enqueue_scripts', 'my_enqueue' ); After localizing your JS file, you can use my_ajax_object object in your JS file: jQuery.ajax( { type: "post", dataType: "json", url: my...

Clear Dropzone.js Thumbnail Image After Uploading An Image

Answer : You can try this: myDropzone.on("complete", function(file) { myDropzone.removeFile(file); }); More information here: http://www.dropzonejs.com/#dropzone-methods removeAllFiles() and removeFile() will trigger the server-side removal too, if you hooked Dropzone to remove files as well. The solution to clear it only client-side, remove the file preview, and if you had a blank state message, remove the dz-started class to prevent the Dropzone CSS from hiding it: $('.dropzone')[0].dropzone.files.forEach(function(file) { file.previewElement.remove(); }); $('.dropzone').removeClass('dz-started'); Another option (similar to answer of Giraldi, but then when all files are completed): myDropzone.on("queuecomplete", function () { this.removeAllFiles(); });

Ajax Add To Cart Button For Product Variation In WooCommerce 3

Answer : To make it work I use a custom ajax add-to-cart for product variations exclusively. 1). I have first changed a bit your button html: <div class="btnss"> <span class="price"> <span class="woocommerce-Price-amount amount">6,999&nbsp; <span class="woocommerce-Price-currencySymbol">kr</span> </span> </span> <div class="quantity buttons_added"> <input type="button" value="-" class="minus"> <label class="screen-reader-text" for="quantity_5b101f605f067">Quantity</label> <input type="number" id="quantity_5b101f605f067" class="input-text qty text" step="1" min="1" max="" name="quantity" value="1" title="Qty" size="4" pattern="[0-9]*" inputm...