json.decoder.JSONDecodeError:期望值:第1行第1列(字符0)ajax请求

时间:2019-01-15 19:28:56

标签: ajax django

我有一个ajax请求,在django视图中,我有以下打印和输出。

我在这一行中有错误,我无法弄清原因。

parsed_json = json.loads(request.body)

我尝试用utf8解码请求正文,但没有任何改变

AJAX通话:

$("#add_user_button").click(function (event) {
    event.preventDefault();  
    $.ajax({
        type: "POST",
        url: '/user/',
        data:
            {
                'action': "addUser",
                'username': $('#id_username').val(),
                'password': $('#id_password').val(),
                'groups': $('#id_groups').val()
            }
        ,
        contentType: 'application/json; charset=utf-8',
        processData: false,
    });
}

DJANGO视图打印:

print(request.POST.get("username"))
#print(request.encoding) #returns none
print("Request body is :")
print(request.body)
print(type(request.body))

输出:

hello
Request body is :
b'username=hello&password=world&csrfmiddlewaretoken=&addUser=Add+User'
<class 'bytes'>

1 个答案:

答案 0 :(得分:0)

如果您分析请求正文,您会发现它不是JSON,要发送JSON,您必须在对象的ajax请求中对其进行编码。

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:custom="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/main.appbar"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/main.collapsing"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">


            <RelativeLayout
                android:id="@+id/sliderr"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax">


                <com.daimajia.slider.library.SliderLayout
                    android:id="@+id/slider"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    custom:auto_cycle="true"
                    custom:indicator_visibility="visible"
                    custom:pager_animation="Accordion"
                    custom:pager_animation_span="1100" />


                <com.daimajia.slider.library.Indicators.PagerIndicator
                    style="@style/AndroidImageSlider_Corner_Oval_Orange"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginBottom="20dp"
                    custom:selected_color="#0095BF"
                    custom:unselected_color="#55ffffff"

                    />



            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="?android:attr/actionBarSize">

                <ImageView
                    android:id="@+id/backs"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="15dp"
                    android:src="@drawable/ic_back"
                    android:tint="@color/white" />


            </RelativeLayout>



        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/laytitel"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/sliderr"
                android:background="@drawable/copongradient"
                android:orientation="vertical">


                <TextView
                    android:id="@+id/resname"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_marginRight="@dimen/text_normal"
                    android:layout_marginTop="5dp"
                    android:text="asdasdasd"
                    android:textColor="#ffffff"
                    android:textSize="18sp" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_alignParentRight="true"
                    android:layout_gravity="right"
                    android:layout_marginBottom="5dp"

                    >


                    <TextView
                        android:id="@+id/descs"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:text="category"
                        android:textColor="@color/white"
                        android:textSize="15sp" />

                    <ImageView
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_alignParentRight="true"
                        android:layout_marginRight="6dp"
                        android:src="@drawable/ic_maps_place"
                        android:tint="@color/white" />


                </LinearLayout>


                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="10dp"
                    android:visibility="gone">

                    <RatingBar
                        android:id="@+id/ratingBar"
                        style="?android:ratingBarStyleSmall"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:layout_marginLeft="1dp"
                        android:layout_marginRight="1dp"
                        android:layout_toLeftOf="@+id/ratingtotal"
                        android:isIndicator="true"
                        android:numStars="5"
                        android:rating="0"
                        android:theme="@style/RatingBar" />

                    <TextView
                        android:id="@+id/foodrating"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="5dp"
                        android:layout_toLeftOf="@+id/ratingBar"
                        android:textColor="@color/white"
                        android:textSize="11sp" />

                    <TextView
                        android:id="@+id/ratingtotal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_marginRight="@dimen/text_normal"
                        android:textColor="@color/white"
                        android:textSize="11sp" />

                </RelativeLayout>


            </LinearLayout>

            <RelativeLayout
                android:id="@+id/tablayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/laytitel">

                <android.support.design.widget.TabLayout
                    android:id="@+id/tablayouts"
                    android:layout_width="match_parent"
                    android:layout_height="43dp"
                    app:tabBackground="@color/white"
                    app:tabGravity="fill"
                    app:tabIndicatorColor="@color/dot_light_screen1"
                    app:tabMaxWidth="0dp"
                    app:tabSelectedTextColor="@color/dot_light_screen1"
                    app:tabTextColor="@color/gray_1" />

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/frame_container"
                    android:layout_below="@+id/tablayouts"
                    />







            </RelativeLayout>




        </RelativeLayout>



    </android.support.v4.widget.NestedScrollView>



    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="25dp"
        android:clickable="true"
        android:src="@drawable/ic_navimap"
        android:tint="@color/colorPrimaryDark"
        app:backgroundTint="@color/white"
        app:layout_anchor="@id/main.appbar"
        app:layout_anchorGravity="bottom|left" />
</android.support.design.widget.CoordinatorLayout>