JavaScript Sending data from JavaScript to Python function locally with AJAX sending data from arduino to raspberry pi,sending data from

I am trying to build a website where a user can enter text, which will be picked up via javascript, and sent to a python function where it will be posted to twitter. For the time being, the python function is being stored locally, along with the rest of the site. However, my AJAX isn't too great and I'm having a few issues.

I have written AJAX code which sends a POST request to the python function with the tweet, and the response is the entire python script. No connection is made to the socket my script is listening to. Below is the AJAX function and the python script. Any ideas what's going on?

Thanks in advance for any help!

$(function(){
    $('#PostTweet').on('click', function(e) {

    var tweet = document.getElementById("theTweet").value;
    var len = tweet.length;

    if(len > 140){
        window.alert("Tweet too long. Please remove some characters");
    }else{

        callPython(tweet);
    }     

    });

});

function callPython(tweet){
 window.alert("sending");
 $.ajax({
 type: "POST",
 url: "tweet.py",
 data: tweet,
 success: function(response){
    window.alert(response);
  }
 })
}

And the Python Script:

from OAuthSettings import settings
import twitter
from socket import *

consumer_key = settings['consumer_key']
consumer_secret = settings['consumer_secret']
access_token_key = settings['access_token_key']
access_token_secret = settings['access_token_secret']

s = socket()
s.bind(('', 9999))
s.listen(4)
(ns, na) = s.accept()

def PostToTwits(data):    
   try:
     api = twitter.Api(
     consumer_key = consumer_key,
     consumer_secret = consumer_secret,
     access_token_key = access_token_key,
     access_token_secret = access_token_secret)

     api.PostUpdate(data)
     makeConnection(s)
   except twitter.TwitterError:
     print 'Post Unsuccessful. Error Occurred'


def makeConnection(s):
    while True:

       print "connected with: " + str(na)
       try:
           data = ns.recv(4096)
           print data
           PostToTwits(data)
       except:
           ns.close()
           s.close()
           break

makeConnection(s)
Answer:1

you need a webserver so that your can make request via web browser.

you can web framework like flask or django or you can use webpy.

A simple example using webpy from their website

import web

urls = (
    '/(.*)', 'hello'
)
app = web.application(urls, globals())

class hello:        
    def GET(self, name):
        if not name: 
            name = 'World'
        return 'Hello, ' + name + '!'

if __name__ == "__main__":
    app.run()

then you call url(your python function) from javascript.

Answer:2

I'm making an html card game and it has an if statement with a greater than or equal to comparison operator. But when the money is lower that the price it still runs as if you have the needed money. ...

I'm making an html card game and it has an if statement with a greater than or equal to comparison operator. But when the money is lower that the price it still runs as if you have the needed money. ...

  1. incorrect result in formulas
  2. incorrect result in testing

As I want to unbind(off) the events I wrapped the code inside a function but as I need to see which key is pressed I need to get the event of the event(not sure how this is called. // normal example ...

As I want to unbind(off) the events I wrapped the code inside a function but as I need to see which key is pressed I need to get the event of the event(not sure how this is called. // normal example ...

I am having to complete a exercise in JavaScript where I use RegExp. During testing, I stumbled across this returned array when I did a non global match. var first = "28".match(/[0-9]\s?/); console....

I am having to complete a exercise in JavaScript where I use RegExp. During testing, I stumbled across this returned array when I did a non global match. var first = "28".match(/[0-9]\s?/); console....

I want to rotate an image every time I click on it.. I've created a function to do this but the image only rotate the first time I click on it... Moreover, once the image rotate it change ...

I want to rotate an image every time I click on it.. I've created a function to do this but the image only rotate the first time I click on it... Moreover, once the image rotate it change ...