Hướng dẫn call python script from javascript with arguments - gọi tập lệnh python từ javascript với các đối số

Vì vậy, tôi đã viết một chương trình bằng JavaScript xuất ra JSON. Những gì tôi muốn làm là, với một nút bấm, tôi muốn chuyển trong JSON đó như một đối số cho một tập lệnh Python.

Tôi không hoàn toàn chắc chắn làm thế nào để tiến hành điều này:

$.ajax[{
url: "/path/to/your/scriptPython",
success: function[response] {
  // here you do whatever you want with the response variable
}
}];

Bất kỳ phản hồi hoặc trợ giúp được đánh giá cao.

Được hỏi ngày 30 tháng 9 năm 2016 lúc 18:25Sep 30, 2016 at 18:25

3

Điều này phụ thuộc vào một cân nhắc quan trọng: Mã Python của bạn ở đâu?

Nếu bạn muốn Python chạy trên máy của người dùng, điều đó trở nên phức tạp do các biện pháp bảo mật: Các trang web thường không được phép thực thi mã tùy ý trên máy của người xem. [Hãy tưởng tượng nếu mã Python đó xóa ổ cứng của họ, ví dụ: trong trường hợp này, tốt nhất là người dùng sẽ tải JSON dưới dạng tệp và sau đó mở nó bằng chương trình Python của bạn.

Nếu bạn muốn Python chạy trên máy chủ của mình, bạn có thể thực hiện yêu cầu bài đăng đến điểm cuối của tập lệnh và chuyển JSON trong dữ liệu bài đăng. Các chi tiết về điều này sẽ phụ thuộc vào sự lựa chọn của bạn về khung web Python [tôi thích bình], nhưng JavaScript sẽ khá giống nhau:

$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];

Đã trả lời ngày 30 tháng 9 năm 2016 lúc 18:44Sep 30, 2016 at 18:44

DraconisdraconisDraconis

3.0291 Huy hiệu vàng15 Huy hiệu bạc27 Huy hiệu đồng1 gold badge15 silver badges27 bronze badges

1

  1. Làm thế nào để
  2. JavaScript Howtos
  3. Gọi Python từ JavaScript

Đã tạo: tháng 3-22, 2022

Gọi tệp máy chủ từ Frontend [HTML] là một trường hợp sử dụng phổ biến cho mọi trang web động. Tất cả các trang web động kết nối với máy chủ thông qua các phương thức khác nhau.

Bài viết này dạy cách gọi Python từ JavaScript.

Sử dụng ajax để gọi Python từ JavaScript

Ajax là viết tắt của JavaScript không đồng bộ và XML. Nó sử dụng đối tượng XMLHttpRequest để giao tiếp với các máy chủ.

Nó có thể gửi và nhận thông tin theo nhiều định dạng, bao gồm các tệp HTML, XML, JSON và văn bản. Tính năng thú vị nhất của Ajax là bản chất không đồng bộ của nó, điều đó có nghĩa là nó có thể giao tiếp với máy chủ, trao đổi dữ liệu và làm mới trang trong khi không phải làm mới trang.

Hai tính năng chính của AJAX cho phép bạn thực hiện bên dưới.

  1. Thực hiện các yêu cầu không đồng bộ vào máy chủ mà không tải lại trang HTML/JavaScript.
  2. Nhận dữ liệu từ máy chủ và xử lý theo yêu cầu của khách hàng.

Syntax:

$.ajax[{
  url: "SERVER_URL",
  data: { param: text },
  type: "HTTP_METHOD",
}].done[function[] {
    /* Process the data */
}];
  1. Phương thức ajax chấp nhận URL dưới dạng tham số đầu vào, một chuỗi chứa URL mà yêu cầu được gửi.
  2. Dữ liệu là một đối tượng JSON chứa các tham số để gửi đến máy chủ. Nếu phương thức HTTP là một phương thức không thể có cơ thể thực thể, chẳng hạn như
    $.ajax[{
        type: 'POST',
        data: my_json_variable,
        url: '//example.com/python_endpoint',
        success: function[response]{ /* deal with the script's output */ }
    }];
    
    0, dữ liệu được thêm vào URL.
  3. $.ajax[{
        type: 'POST',
        data: my_json_variable,
        url: '//example.com/python_endpoint',
        success: function[response]{ /* deal with the script's output */ }
    }];
    
    1 là phương thức HTTP được sử dụng để yêu cầu máy chủ.
  4. $.ajax[{
        type: 'POST',
        data: my_json_variable,
        url: '//example.com/python_endpoint',
        success: function[response]{ /* deal with the script's output */ }
    }];
    
    3 là một cấu trúc thay thế cho tùy chọn gọi lại thành công và vui lòng tham khảo
    $.ajax[{
        type: 'POST',
        data: my_json_variable,
        url: '//example.com/python_endpoint',
        success: function[response]{ /* deal with the script's output */ }
    }];
    
    4 để biết chi tiết thực hiện.

Example:

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];

Trong mã trên, chúng tôi thực hiện các yêu cầu

$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
5 cho máy chủ với tham số là
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
6. Nó sẽ gọi tệp python
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
7, sẽ chấp nhận các tham số đầu vào và xử lý dữ liệu.

Sau đó, nó sẽ trả lại phản hồi cho khách hàng.

Output:

Thank you.

$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
8
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
9

$.ajax[{
  url: "SERVER_URL",
  data: { param: text },
  type: "HTTP_METHOD",
}].done[function[] {
    /* Process the data */
}];
0
$.ajax[{
  url: "SERVER_URL",
  data: { param: text },
  type: "HTTP_METHOD",
}].done[function[] {
    /* Process the data */
}];
1

$.ajax[{
  url: "SERVER_URL",
  data: { param: text },
  type: "HTTP_METHOD",
}].done[function[] {
    /* Process the data */
}];
2
$.ajax[{
  url: "SERVER_URL",
  data: { param: text },
  type: "HTTP_METHOD",
}].done[function[] {
    /* Process the data */
}];
3
$.ajax[{
  url: "SERVER_URL",
  data: { param: text },
  type: "HTTP_METHOD",
}].done[function[] {
    /* Process the data */
}];
4
$.ajax[{
  url: "SERVER_URL",
  data: { param: text },
  type: "HTTP_METHOD",
}].done[function[] {
    /* Process the data */
}];
5
$.ajax[{
  url: "SERVER_URL",
  data: { param: text },
  type: "HTTP_METHOD",
}].done[function[] {
    /* Process the data */
}];
6

$.ajax[{
  url: "SERVER_URL",
  data: { param: text },
  type: "HTTP_METHOD",
}].done[function[] {
    /* Process the data */
}];
0
$.ajax[{
  url: "SERVER_URL",
  data: { param: text },
  type: "HTTP_METHOD",
}].done[function[] {
    /* Process the data */
}];
8

$.ajax[{
  url: "SERVER_URL",
  data: { param: text },
  type: "HTTP_METHOD",
}].done[function[] {
    /* Process the data */
}];
2
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
0
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
1
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
2

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
4
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
5

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
7
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
5

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
Thank you.
0
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
5

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
Thank you.
3
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
5

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
Thank you.
6
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
5

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
Thank you.
9
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
5

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3ajax2
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
5

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3ajax5
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
5

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3ajax8
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
5

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3XMLHttpRequest1
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
5

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3XMLHttpRequest4
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
5

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3XMLHttpRequest7
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
5

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3ajax0
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
5

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3ajax3 ajax4
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
5

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3ajax4
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
5

$.ajax[{
  url: "SERVER_URL",
  data: { param: text },
  type: "HTTP_METHOD",
}].done[function[] {
    /* Process the data */
}];
2JSON0

$.ajax[{
  url: "SERVER_URL",
  data: { param: text },
  type: "HTTP_METHOD",
}].done[function[] {
    /* Process the data */
}];
2JSON2
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
1
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
2

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3JSON6JSON7 JSON8

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3HTTP0JSON7 HTTP2

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3HTTP4

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3HTTP6
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
5

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3HTTP9
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
5

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
02

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
04

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
06

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
08JSON7
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
10

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
12

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
14
$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
5

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
17

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
19

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
21
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
22
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
23

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
25
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
26
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
27
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
28
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
29

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
31

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
33
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
34
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
35

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
37
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
38
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
39

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
41

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
43
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
22
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
45

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
47

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
49
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
22
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
51

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
53

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
55
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
56
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
57

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
59

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
61
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
62

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
64

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
66JSON7
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
68

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
70

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
72

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
74
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
75
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
76
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
22
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
78

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
80

$.ajax[{
  url: "SERVER_URL",
  data: { param: text },
  type: "HTTP_METHOD",
}].done[function[] {
    /* Process the data */
}];
2JSON0

$.ajax[{
  url: "SERVER_URL",
  data: { param: text },
  type: "HTTP_METHOD",
}].done[function[] {
    /* Process the data */
}];
2
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
84
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
85

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
87

$.ajax[{
  url: "SERVER_URL",
  data: { param: text },
  type: "HTTP_METHOD",
}].done[function[] {
    /* Process the data */
}];
2
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
89
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
90

$.ajax[{
  type: "POST",
  url: "~/helloWorld.py",
  data: {
    param: "hello world",
  }
}].done[[o] => {
   console.log[o]
}];
3
$.ajax[{
    type: 'POST',
    data: my_json_variable,
    url: '//example.com/python_endpoint',
    success: function[response]{ /* deal with the script's output */ }
}];
92

Tôi có thể gọi một tệp python từ javascript không?

Không, trình duyệt [may mắn thay] sẽ không thực thi mã Python tùy ý.Bạn sẽ muốn chạy nó trong một máy chủ.JavaScript chạy trên máy khách.. You'll want to run that in a server. Javascript runs on the client.

Tôi có thể sử dụng Python và JavaScript cùng nhau không?

Nó có thể được sử dụng để phát triển máy tính để bàn cũng như các ứng dụng web.Q #5] Tôi có thể học JavaScript và Python cùng một lúc không?Trả lời: Có, cả JavaScript và Python đều có thể được học cùng một lúc.JavaScript chủ yếu được sử dụng để lập trình mặt trước trong khi Python được sử dụng để lập trình phụ trợ.Yes, Javascript and Python can both be learned at the same time. Javascript is mainly used for front-end programming whereas Python is used for backend programming.

Làm thế nào để bạn gọi một hàm python từ ajax?

Bạn có thể làm điều này với jQuery //api.jquery.com/jquery.ajax/ hoặc chỉ sử dụng javascript $ .ajax [{type: "post", url: "~/pythoncode.py": Text}}]. Được thực hiện [hàm [o] {// làm điều gì đó}];Có một loạt các cách tiếp cận có thể được thực hiện để giải quyết vấn đề tương tự gọi hàm Python từ JavaScript.$. ajax[{ type: "POST", url: "~/pythoncode.py", data: { param: text} }]. done[function[ o ] { // do something }]; There are a variety of approaches that can be taken to solve the same problem Call Python Function From Javascript.

Bài Viết Liên Quan

Chủ Đề