javascript - Uncaught TypeError: d.type.toUpperCase is not a function - Stack Overflow

so I'm trying to send data as a json to a Python file, however my Python I'm getting the erro

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
 |  Show 1 more ment

1 Answer 1

Reset to default 4

You 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 for method. You should use type 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745415843a4626745.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信