django跨域请求

views.py:

from django.shortcuts import render, HttpResponse
from django.http import JsonResponse
from .libvirtapi import *
from django.views.decorators.csrf import csrf_exempt
import json

@csrf_exempt
def get_content(request):
    json_data = request.body.decode('utf-8')
    data = json.loads(json_data)
    hostip = data['hostip']
    instance_name =  data['instance_name']
    url = 'qemu+tcp://{}/system'.format(hostip)
    Libapi = Libvirt(url, instance_name)
    return JsonResponse({})

发送端:

import requests,json

def postabc(data):
    headers = {'Content-Type': 'application/json','Accept': '*/*'}
    url = 'http://127.0.0.1:8002/vm/'
    data = json.dumps(data).encode('utf-8')
    requests.post(url,headers =headers, data = data)

或者

from django.shortcuts import render, HttpResponse
from django.http import JsonResponse
from .libvirtapi import *
from django.views.decorators.csrf import csrf_exempt
import json

@csrf_exempt
def get_content(request):
    data = request.POST
    hostip = data['hostip']
    instance_name =  data['instance_name']
    url = 'qemu+tcp://{}/system'.format(hostip)
    Libapi = Libvirt(url, instance_name)
    return JsonResponse({})

发送端:

import requests,json

def postabc(data):
    url = 'http://127.0.0.1:8002/vm/'
    requests.post(url, data = data)

你可能感兴趣的:(Django)