so I'm trying to send data as a json to a Python file, however my Python I'm getting the error
Uncaught TypeError: d.type.toUpperCase is not a function
I'm a newbie to JS so I'm not sure how everything fully works, however I did just add in the GET
by the POST
because before that I was getting 405
errors.
Now, I'm getting this d.type.toUpperCase
error. Here is my code. Please help!!
JavaScript:
function on_request_success(response) {
console.debug('response', response);
document.write("Success!!!");
}
function on_request_error(r, text_status, error_thrown) {
console.log(r);
console.debug('error' + text_status + ", " + error_thrown + ":\n" + r.responseText);
document.write("Failure line 11");
}
var request = {"Data":"Success!!"};
function addTrack() {
$.ajax({
url: '.py',
type: ['GET','POST'],
cache: false,
data: JSON.stringify(request),
contentType: 'application/json',
processData: false,
success: on_request_success,
error: on_request_error
});
}
Python:
import json
import sys
request = json.load(sys.stdin)
file_open = open('.txt', a)
file.write(request)
file.close()
so I'm trying to send data as a json to a Python file, however my Python I'm getting the error
Uncaught TypeError: d.type.toUpperCase is not a function
I'm a newbie to JS so I'm not sure how everything fully works, however I did just add in the GET
by the POST
because before that I was getting 405
errors.
Now, I'm getting this d.type.toUpperCase
error. Here is my code. Please help!!
JavaScript:
function on_request_success(response) {
console.debug('response', response);
document.write("Success!!!");
}
function on_request_error(r, text_status, error_thrown) {
console.log(r);
console.debug('error' + text_status + ", " + error_thrown + ":\n" + r.responseText);
document.write("Failure line 11");
}
var request = {"Data":"Success!!"};
function addTrack() {
$.ajax({
url: 'http://mattprice09.github.io/addTrack.py',
type: ['GET','POST'],
cache: false,
data: JSON.stringify(request),
contentType: 'application/json',
processData: false,
success: on_request_success,
error: on_request_error
});
}
Python:
import json
import sys
request = json.load(sys.stdin)
file_open = open('http://mattprice09.github.io/database.txt', a)
file.write(request)
file.close()
Share
Improve this question
edited Sep 19, 2015 at 10:21
repeat
18.7k4 gold badges57 silver badges166 bronze badges
asked Sep 19, 2015 at 9:36
MattMatt
1915 silver badges14 bronze badges
6
- You can't open a URL for writing to; that's not a file. Not that that causes the error you posted, the Python code you posted cannot raise it. What is the full traceback of the exception? Are you sure you are calling the right URL? – Martijn Pieters Commented Sep 19, 2015 at 9:38
-
Besides, github.io hosting doesn't allow for Python scripts, so
http://mattprice09.github.io/addTrack.py
is certainly not executed. – Martijn Pieters Commented Sep 19, 2015 at 9:39 - Ok, thanks. I'll figure out how to open a url instead of file. The full stack is: f.extend.ajax @ jquery.min.js:4 addTrack @ tester.js:17 (anonymous function) @ index.html:12 – Matt Commented Sep 19, 2015 at 9:40
-
The
type
parameter for the$.ajax
call is also not supposed to be a list, I think. Too many things are badly broken here. – Martijn Pieters Commented Sep 19, 2015 at 9:41 - Ah, and that's exactly why you get the error, it is a JS error telling you a list cannot be uppercased.. – Martijn Pieters Commented Sep 19, 2015 at 9:42
1 Answer
Reset to default 4You need to set the type
argument to a string, not a list:
type: 'POST',
From the $.ajax()
documentation:
type (default:
'GET'
)
Type: String
An alias formethod
. You should usetype
if you're using versions of jQuery prior to 1.9.0.
The error tells you that a list cannot be uppercased.
However, you'll have other major issues here:
- GitHub pages doesn't support server-side scripts. Your Python code is not going to be executed.
- You cannot open a remote URL for writing; the
open()
function can only open files on the local filesystem.