You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django项目中jQuery Ajax请求URL重复问题求助

Fixing the Duplicated Ajax Request URL in Your Django Project

Hey there! Let's break down why your Ajax request URL is showing up as http://127.0.0.1:8000/myportfolio/add_transaction/myportfolio/add_transaction instead of the expected http://127.0.0.1:8000/myportfolio/add_transaction/—and how to fix it.

The Root Cause: Relative vs. Absolute Paths

The most likely issue is that you're using a relative path in your jQuery Ajax code, and your current page URL is already http://127.0.0.1:8000/myportfolio/add_transaction/.

For example, if your Ajax code looks something like this:

$.ajax({
    url: 'myportfolio/add_transaction', // No leading slash
    method: 'GET',
    // ... other settings
});

Browsers will interpret this relative path as "append this to the current page's URL", resulting in the duplicated path you're seeing.

Quick Fixes to Correct the URL

1. Use an Absolute Path (Simple & Direct)

Add a leading slash to your Ajax URL to tell the browser to start from the root of your domain:

$.ajax({
    url: '/myportfolio/add_transaction/', // Leading slash = root-relative
    method: 'GET',
    // ... other settings
});

This will point directly to the correct endpoint every time, regardless of the current page's URL.

Hardcoding URLs can lead to issues later if you change your urls.py paths. Instead, use Django's built-in URL reversing to dynamically get the correct URL:

First, make sure your urls.py has a name for the endpoint:

# urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('myportfolio/add_transaction/', views.add_transaction, name='add_transaction'),
    # ... other paths
]

Then, in your template, pass this URL to your JavaScript code:

<!-- In your Django template -->
<script>
// Capture the reversed URL in a JS variable
const addTransactionUrl = "{% url 'add_transaction' %}";
</script>

Now use this variable in your Ajax call:

$.ajax({
    url: addTransactionUrl,
    method: 'GET',
    // ... other settings
});

This ensures your URL always matches what's defined in urls.py, even if you update the path later.

Bonus: Don't Forget to Handle GET Requests in Your View

You mentioned you haven't modified your view to adapt to GET requests—once the URL is fixed, update your view to handle GET properly. For example:

# views.py
from django.http import JsonResponse

def add_transaction(request):
    if request.method == 'GET':
        # Add your GET request logic here (e.g., fetch data to return)
        return JsonResponse({
            'status': 'success',
            'message': 'GET request handled correctly'
        })
    # Handle other request methods (POST, etc.) here if needed
    return JsonResponse({'status': 'error', 'message': 'Invalid request method'})

内容的提问来源于stack exchange,提问作者SkillSet12345

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:55:50