Ajax query format with Elasticsearch
I am trying to make a post request with AJAX to my elasticsearch index. The cURL result is:
[~]$ curl -XGET 'http://localhost:9200/firebase/_search?q=song:i%20am%20in'
{"took":172,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":0.82749283,"hits":[{"_index":"firebase","_type":"song","_id":"0001","_score":0.82749283,"_source":{"song":"i am in california","song_name":"hello","song_url":"https://s3.ap-south-1.amazonaws.com/songapp-dump/media/songs/Adele_-_Hello-_i_am_in_california.mp3"}},{"_index":"firebase","_type":"song","_id":"0002","_score":0.28582606,"_source":{"song":"i must have called a thousand times","song_name":"hello","song_url":"https://s3.ap-south-1.amazonaws.com/songapp-dump/media/songs/Adele_-_Hello-_i_must_have_called_a_thousand_times.mp3"}}]}}
Browser result is:This is also working correctly. Meaning the index has been created and cURL/ GET is able to get the result.
When I am trying to have an AJAX request do the same, I am struggling with the query format probably. I am not able to figure out.
Ajax.js
$(function() {
$('#message').keyup(function() {
// console.log(JSON.stringify());
var data = {
'song': $('#message').val()
};
console.log(JSON.stringify(data));
$.ajax({
type: "POST",
url: "http://localhost:9200/firebase/_search",
contentType: 'application/json',
// data: {
// 'q': $('#message').val()
// },
data: JSON.stringify(data),
success: searchSuccess,
dataType: 'jsonp'
});
});
});
The console logs the following error:
Basically it's a 400 Bad Request error. I am not able to figure out if there is something wrong with my query or the way Ajax request is being created. Why am I having callback issues! Any help would be appreciated. I have scoured the web on this issue and have tried various combinations as well.
1 answer
-
answered 2017-06-17 18:47
styfle
Change the method to
GET
and dateType tojson
. Also the querystring requires aq
parameter.var data = { 'q': 'song:' + $('#message').val() }; $.ajax({ type: "GET", url: "http://localhost:9200/firebase/_search", contentType: 'application/json', data: JSON.stringify(data), success: searchSuccess, dataType: 'json' });
See also questions close to this topic
-
console.log looping json data but display not
I'm trying to display json data on the page, however when sending the output to html it only displays the last id.
When using
console.log
it loops through each id available but not on the page output itselfvar username = $("#usernameinfo").text(); $.ajax({ type: "GET", dataType: 'json', url: "<?= base_url()?>"+"account/listings/more_user_ads", data: { "username": username, "pid": "<?=$this->input->get('pid')?>" }, success: function(res){ for (var i = 0; i < res.length; i++){ var output = ""; var object = res[i]; output = "<p>" + object.id + "</p>"; $("#tg-authoradsslider").html(output); // outputs only the id of 3 which is the last id in the loop console.log(output); /* consoles.logs <p>1</p> <p>2</p> <p>3</p>*/ } } });
-
ReactJS Form Submit keeps causing refresh of page
Learning basics of react here.
I have a simple Zipcode form I'm trying to update the Zipcode in the form component I made and in the app parent component I made.
In my onSumbit prop I call a function to update and added
e.preventDefault();
but the submit button still refreshes everything and i'm pretty sure loses my data.
I commented out my onInput functions and props because I realize it's not affecting my onSubmit action but let me know if that's needed in React for some reason.
I can add more if needed. In my index I am using bootstrap4 cdn
App.js:
import React, { Component } from 'react'; import GMap from './GMap'; import ZipCode from './ZipCode'; import './css/App.css'; class App extends Component { constructor(props){ super(props); this.state = {zipcode : ''}; this.onZipCodeChange = this.onZipCodeChange.bind(this); } onZipCodeChange(e){ e.preventDefault(); console.log('App has detected ZipCode Change'); const value = e.target; this.setState({zipcode: value}); } render() { return ( <div className="App"> <header> <nav className="navbar navbar-expand-lg navbar-light bg-light"> <a className="navbar-brand" href="#">Navbar</a> <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span className="navbar-toggler-icon"></span> </button> <div className="collapse navbar-collapse" id="navbarNav"> <ul className="navbar-nav"> <li className="nav-item active"> <a className="nav-link" href="#">Home <span className="sr-only">(current)</span></a> </li> <li className="nav-item"> <a className="nav-link" href="#">Features</a> </li> <li className="nav-item"> <a className="nav-link" href="#">Pricing</a> </li> <li className="nav-item"> <a className="nav-link disabled" href="#">Disabled</a> </li> </ul> </div> </nav> </header> <ZipCode onSubmit={this.onZipCodeChange} /> <GMap /> </div> ); } } export default App;
ZipCode.js:
import React, { Component } from 'react'; import './css/App.css'; class ZipCode extends Component { constructor(props) { super(props); this.state = {zipcode: ''}; // this.updateZip = this.updateZip.bind(this); this.submitZipCode = this.submitZipCode.bind(this); } componentDidMount() { console.log(" ZipCode will mount"); } componentWillUnmount() { console.log("ZipCode will unmount"); } submitZipCode(e){ e.preventDefault(); const { value } = this.state; this.setState({zipcode: value}); const { onSubmit } = this.props; //pull out to call method it is linked to console.log('submitting zipcode'); onSubmit(value); } // updateZip(e){ // const value = e.target; // this.setState({ zipcode : value }); // console.log('zipcode updated to: '+ this.state.zipcode.value); // } // onInput={this.updateZip} render(){ return( <div className="row"> <div className="col-sm-4 col-md-4 col-lg-4"> <form onSubmit={this.submitZipCode}> <label>Zip Code</label> <input type="input" name="zipcode" value={this.zipcode} /> <button type="submit" className='btn btn-success'>Submit</button> </form> </div> </div> ); } } export default ZipCode;
-
keydown event multiple key hold odd behavior
I have been running the following code using es6 babel on chrome:
window.hold = []; window.addEventListener('keydown', (e) => { if (!window.hold.includes(e.key)) { setTimeout(() => { console.log("> - " + e.key); window.hold.push(e.key); }, 0); } else { e.preventDefault(); } }); window.addEventListener('keyup', (e) => { setTimeout(() => { console.log("< - " + e.key); window.hold.splice(window.hold.indexOf(e.key), 1); e.preventDefault(); }, 0); });
If I hold ASDZ on the keyboard, it doesn't log the Z, but if I hold ASDK, it logs everything as it should, does anyone know why this is happening?
-
Bootstrap form input field that requires at least one element to be selected?
I have form where I would like to create input field(s) for account type. This is little tricky since there might be only one account type but can be two as well. In other words when first, last name is entered then they have to choose either User or Staff for account type or they can pick both. Once they select either or both then additional fields will show int he form. My question is what is the best option to approach that would require user to select at least one account type but at the same time allow them to choose two as well if they need to? Also I use HTML5 validation for this purpose, so if they choose only one the other gonna be hidden. Those hidden form fields should not be required but now I don't have a way to prevent that. Here is example:
<script language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script language="javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <form name="frmSave" id="frmSave" class="frm-Submit" autocomplete="off"> <div class="form-group"> <input type="text" class="form-control" name="frm_firstname" id="frm_firstname" placeholder="Enter First Name" maxlength="50" required> </div> <div class="form-group"> <input type="text" class="form-control" name="frm_lastname" id="frm_lastname" placeholder="Enter Last Name" maxlength="50" required> </div> <div class="form-group required"> <button class="btn btn-primary" data-toggle="collapse" data-target="#user-account">User Account</button> </div> <div id="user-account" class="collapse"> <div class="form-group required"> <input type="text" class="form-control" name="frm_username" id="frm_username" placeholder="Enter UserName" maxlength="50" required> </div> </div> <div class="form-group required"> <button class="btn btn-primary" data-toggle="collapse" data-target="#staff-account">Staff Account</button> </div> <div id="staff-account" class="collapse"> <div class="form-group required"> <div class="input-group"> <input type="text" class="form-control" name="frm_position" id="frm_position" placeholder="Choose Position" required> </div> </div> </div> <button type="submit" name="frmSaveaccount_submit" id="frmSaveaccount_submit" class="btn btn-primary">Submit</button> </form>
So buttons for User and Staff account are actually separate fields in my database and I have to save values for example 1 or 0 if they have entered User information or Staff. Also if I try to submit the form with only Staff information for example I can't since User form fields are hidden but still required. How that can be fixed so only visible fields are required?
-
JQGrid Search Dialog: text input resizes when selecting column from droplist
I'm using jqGrid's search dialog box and resizing the text input in the beforeShowSearch and afterRedraw events as per this Stack Overflow post.
That works great on the initial load and reset, however, as mentioned in the comments of that post's answer, the text input resizes its width to default when a new value is chosen from the column droplist. How does one maintain the width of the text input upon selection of a new droplist item?
-
tinymce fullscreen issue with jquery dialog
TinyMCE in fullscreen is not going full screen to the window, it gets cutoff inside the jquery dialog.
I've tried changing z-index but no luck.
Also tried this but didn't work:
.ui-dialog { -webkit-transform: none; -ms-transform: none; -o-transform: none; transform: none; }
-
Is it possible to call AJAX multiple times with updated url data?
In JavaScript, I'm trying to
- Loop through ajax call N number of times
- This N number is returned AFTER the first ajax call (since the loading_element is displayed when pagination=1)
- If pages=5 after first ajax call, loop through and call url using pagination=2, pagination=3, pagination=4, pagination=5
Here's my issue: I'm wondering if it's possible to restart an ajax call or maybe even restart a for loop after getting the updated "pages" value? Thanks so much, any help is appreciated!
var pages = parseInt(loading_element.attr("data-pages"),10), pages_loaded = 0; for (var page=1; page<=pages; page++) { $.ajax({ url:String(document.location).replace("/search/","/search-ajax/"), data:"pagination=" + page, dataType:$.browser.msie ? "text" : "xml", type:"GET", success: function(data, url) { // pages element is returned when pagination=1 } }
-
Django refresh page
I can not figure out how to change the page without rebooting with Ajax.
urls.py
urlpatterns = [ path('name/', views.name, name='name'), path('', views.index, name='index'), ]
views.py
def name(request): return render(request, 'site/name.html')
index.html
<a href="/name" id="aj">page name</a> <div id="content"> {% block content %} {% endblock %} </div>
I will be very grateful if you will give me a simple code.
-
Display JSON data with Ajax jQuery not working
This is my JSON data structure:
{ "staff_id":"7", "staff_name":"John Hilton", "client_name":"Saxo Pvt. Ltd", "assign_date":"20-04-2018", "work_purpose":"bank analysis", "duedate":"19-04-2018", "returndate":"04-04-2018", "period":"monthly", "remarks":"helloooooooooo" }
And I want to display with jQuery, but it is not working:
function assigngetid(id){ $("#id").val(id); $.post("controller/workprocess_edit_ajax.php",{ action:'detail', id:id, dataType:"json", },function(data){ $("#remarks").val(data[0]); }); }
How can I fix this?
-
Google Cloud Identity Aware Proxy (App Engine) - Strange web browser behavior?
I am seeing some strange behavior using App Engine with Identity Aware Proxy in Chrome (Desktop & Mobile) / Firefox (Desktop & Mobile) / Safari (Desktop) / curl (Desktop)
I launched a static-file site on App Engine using these settings
app.yaml:
runtime: python27 api_version: 1 threadsafe: true handlers: - url: /(.*) static_files: index.html upload: index.html secure: always
index.html:
<html> <body> Hello World! </body> </html>
I then used the cloud console to enable the Identity Aware Proxy.
As expected, I was asked to sign in using the google account needed to access the page. All good.
However, sometimes I can access the site from a browser without credentials, or even from
curl
, which I feel should definitely not be possible?It takes a bunch of refreshes / retries, but once it is reproduced I can reliably get the index page without authentication using Chrome, Firefox, Opera, and
curl
.Questions:
- Am I doing something completely stupid? Is it expected behavior to sometimes be able to access the page even in incognito/private mode, or using
curl
? - I know there is a default 10 minute caching header on static files served by App Engine, how does that factor in?
- How does
curl
get mixed up in all of this? AFAIK https can not be cached by anyone except the UA making the request (and internally on Google's end)? Is there a cache on my computer that all of these sources talk to that I am not aware of? - Is this a problem on my computer/phone (i.e. once the page is cached somehow all UAs on that device can see the page without authenticating)?
- Is this a problem on Google's end?
For completeness, here's the output from
curl -v
curl -v https://xxxxxxxxxxxx.appspot.com * Rebuilt URL to: https://xxxxxxxxxxxx.appspot.com/ * Trying 172.217.22.180... * TCP_NODELAY set * Connected to xxxxxxxxxxxx.appspot.com (172.217.22.180) port 443 (#0) * ALPN, offering h2 * ALPN, offering http/1.1 * Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH * successfully set certificate verify locations: * CAfile: /etc/ssl/cert.pem CApath: none * TLSv1.2 (OUT), TLS handshake, Client hello (1): * TLSv1.2 (IN), TLS handshake, Server hello (2): * TLSv1.2 (IN), TLS handshake, Certificate (11): * TLSv1.2 (IN), TLS handshake, Server key exchange (12): * TLSv1.2 (IN), TLS handshake, Server finished (14): * TLSv1.2 (OUT), TLS handshake, Client key exchange (16): * TLSv1.2 (OUT), TLS change cipher, Client hello (1): * TLSv1.2 (OUT), TLS handshake, Finished (20): * TLSv1.2 (IN), TLS change cipher, Client hello (1): * TLSv1.2 (IN), TLS handshake, Finished (20): * SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256 * ALPN, server accepted to use h2 * Server certificate: * subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=*.appspot.com * start date: Mar 28 14:17:04 2018 GMT * expire date: Jun 20 13:24:00 2018 GMT * subjectAltName: host "xxxxxxxxxxxx.appspot.com" matched cert's "*.appspot.com" * issuer: C=US; O=Google Trust Services; CN=Google Internet Authority G3 * SSL certificate verify ok. * Using HTTP2, server supports multi-use * Connection state changed (HTTP/2 confirmed) * Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0 * Using Stream ID: 1 (easy handle 0x7ff81780a400) > GET / HTTP/2 > Host: xxxxxxxxxxxx.appspot.com > User-Agent: curl/7.54.0 > Accept: */* > * Connection state changed (MAX_CONCURRENT_STREAMS updated)! < HTTP/2 200 < date: Fri, 20 Apr 2018 17:43:10 GMT < expires: Fri, 20 Apr 2018 17:53:10 GMT < etag: "8wDEQg" < x-cloud-trace-context: 8e9c1b6803383aac532d48d9f0ac5fc2 < content-type: text/html < content-encoding: gzip < server: Google Frontend < cache-control: public, max-age=600 < content-length: 54 < age: 371 < alt-svc: hq=":443"; ma=2592000; quic=51303433; quic=51303432; quic=51303431; quic=51303339; quic=51303335,quic=":443"; ma=2592000; v="43,42,41,39,35" < ���(�ͱ�I�O��� * Connection #0 to host xxxxxxxxxxxx.appspot.com left intact I-.Q�ч�l�!����Z�_$%
The output above SHOULD show a 302 redirect to IAP's login page, but as previously stated - it does not always do that!
TL;DR Why can I access App Engine static pages protected by IAP on my computer from contexts that should not be allowed access?
Thanks!
- Am I doing something completely stupid? Is it expected behavior to sometimes be able to access the page even in incognito/private mode, or using
-
Run a dag with Airflow via curl and Airflow's experimental rest api
I'm trying to execute a dag within airflow by using a curl command. I am trying to do this via the Experimental Rest API documented here:
https://airflow.apache.org/api.html
Here is my curl command that I am trying to execute:
curl -X POST -v -u user@somedoman.com:somepassword -d '' 'https://airflow.somedomain.com/api/experimental/dags/my_dag_id/dag_runs'
When I run this command, I get a 400 Bad Request -- The browser (or proxy) sent a request that this server could not understand
The call I am attempting appears to conform with the api documentation, so I am confused on what I'm doing wrong.
-
invalid grant type- oauth 2.0- obtaining token
I have been struggling with the following code for some time. I get the following error: {"error":"invalid_request","error_description":"invalid grant type"}.
Some more documentation on the API that I am working on is available here::
<?php $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "https://id.shoeboxed.com/oauth/token", CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_POST => 1, CURLOPT_POSTFIELDS => "{\"code\": \"['code']\",\"grant_type\":\"authorization_code\",\"redirect_uri\": \"http://website.com/foursquare2.php\",\"client_id\": \"f8de67be8dc84e449203fcdd4XXXXXXX\",\"client_secret\": \"HS5ZeIVsKW0/qqiO9/XcdeWqnF8vtzQrpY8gcdrxg0BXNZXXXXXXX\"}", CURLOPT_HTTPHEADER => array( "application/x-www-form-urlencoded" ) )); /* //Another Attempt at it is below curl -d code=['code'] \ -d grant_type=authorization_code \ --data-urlencode redirect_uri='http://website.com/foursquare2.php' \ -u f8de67be8dc84e449203fcdd44abad5a:HS5ZXXXXXXX/qqiO9/XcdeWqnF8vtzQrpY8gcdrxg0BXNXXXXXXX \ -XPOST https://id.shoeboxed.com/oauth/token */ $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { echo $response; } ?>
-
Updating The Whole Selected Elastic Record
Using
olivere/elastic
to connect my Go (1.10) to my Elastic search, which is running in a Docker container.Now when the database has an update it will run a trigger to my API which should search for that record in my Elastic search, if not found add a new record or if found update the whole record.
Now I have the add new record code in place and its working but I can not see how to update the whole record, selected elements of that record I can but not the whole record.
Here is what I have:
testData := []byte(`{"data1":"22222","data2":"ddddd"}`) script := elastic.NewScript("ctx._source = params.newCont").Params(map[string]interface{}{"newCont": testData}) put1, _ := client.Update(). Index("myindex"). Type("mytype"). Id("id-here"). Script(script). Do(ctx) fmt.Println( put1 )
Now when I update
_source
to something like_source.data1
I can update that but I want mytestData
var to update all records within this id. ThetestData
var is just for testing, I will be loading this from the database, as I have no idea what would have changed, I need the whole record updated?Thanks.
-
Can't index data with elasticsearch due to network error
When I try to index a data from logstash I receive this error that may relate to network issues:
[2018-04-20T14:40:16,532][WARN ][o.e.h.n.Netty4HttpServerTransport] [SST-P1307000FW] caught exception while handling client http traffic, closing connection [id: 0xfcf68e19, L:/127.0.0.1:9200 - R:/127.0.0.1:64088] java.io.IOException: Une connexion existante a dû être fermée par l’hôte distant at sun.nio.ch.SocketDispatcher.read0(Native Method) ~[?:?] at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43) ~[?:?] at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223) ~[?:?] at sun.nio.ch.IOUtil.read(IOUtil.java:197) ~[?:?] at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380) ~[?:?] at io.netty.buffer.PooledHeapByteBuf.setBytes(PooledHeapByteBuf.java:261) ~[netty-buffer-4.1.11.Final.jar:4.1.11.Final] at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1100) ~[netty-buffer-4.1.11.Final.jar:4.1.11.Final] at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:372) ~[netty-transport-4.1.11.Final.jar:4.1.11.Final] at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:123) [netty-transport-4.1.11.Final.jar:4.1.11.Final] at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:644) [netty-transport-4.1.11.Final.jar:4.1.11.Final] at io.netty.channel.nio.NioEventLoop.processSelectedKeysPlain(NioEventLoop.java:544) [netty-transport-4.1.11.Final.jar:4.1.11.Final] at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:498) [netty-transport-4.1.11.Final.jar:4.1.11.Final] at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:458) [netty-transport-4.1.11.Final.jar:4.1.11.Final] at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858) [netty-common-4.1.11.Final.jar:4.1.11.Final] at java.lang.Thread.run(Thread.java:748) [?:1.8.0_144]
-
Python Pyramid(cornice) with Elasticsearch DSL
Using python pyramid and ElastiSearch. I looked at pythonelasticsearch-dsl which offers a nice ORM but I'm not sure how to integrate it with pyramid.
So far I made a "global connection" as per pythonelasticsearch-dsl and expose the connection via an attribute into pyramid's request.
Do you see anything wrong with this code ?!
def _create_es_connection(config): registry = config.registry settings = registry.settings es_servers = settings.get('elasticsearch.' + 'servers', ['localhost:9200']) es_timeout = settings.get('elasticsearch.' + 'timeout', 20) registry.es_connection = connections.create_connection( hosts=es_servers, timeout=es_timeout) def get_es_connection(request): return getattr(request.registry, 'es_connection', connections.get_connection()) # main def main(global_config, **settings): ... config = Configurator(settings=settings) config.add_request_method( get_es_connection, 'es', reify=True)
I use the connection as
#view request.es ...
If there are any other ways I would appreciate any pointers - thank you.