Home > Mobile >  Django : Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
Django : Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

Time:05-15

Error When Click Button for send data. Please Helping me to solve problem. Error When Click Button for send data. Please Helping me to solve problem. Error When Click Button for send data. Please Helping me to solve problem.

detail.html

    td>
              <!-- <a href="{% url 'Detail_pem' %}"><button data-product="{{order.id}}" data-act="{{order.name}}"  >Detail</button> </a> -->
              <button data-product="{{order.id}}" data-act="{{order.name}}"  >Detail</button> 
            </td>
          </tr>
          {% endfor %}
        </tbody>
      </table>
</div>
<!-- <script type="text/JavaScript" src="{% static 'js/pem.js' %}"></script> -->

<script>
var id_order = document.getElementsByClassName('id_order')
for (i = 0; i < id_order.length; i  ) {
  id_order[i].addEventListener('click', function(){
        var orid  = this.dataset.product
        var ornm  = this.dataset.act
        console.log('orid :', orid)
        console.log('ornm :', ornm)
        codata(orid, ornm)
    })
    
}
function codata(orid, ornm){
  console.log('orid :', orid, 'ornm :', ornm)
const url = "Detail"
fetch(url, {
        method :'POST',
        headers : {
            'Content-Type' : 'application/json',
            'X-CSRFToken'  :  csrftoken,
        },
        body:JSON.stringify({'orid':orid, 'ornm':ornm}),
    })
    .then((response) =>{
        return response.json();
    })
    .then((data) => {
      console.log('Success:', data);
                })
}
</script>
{% endblock %}

view.py

def Detail(request):
    data    = json.loads(request.body.decode("utf-8"))
    orid    = data['orid']
    ornm    = data['ornm']
    print('id :', orid,'nama :', ornm)
    context = {'orid ':orid , 'ornm':ornm}

    return render(request, 'store/detail.html', context )

CodePudding user response:

I think that the problem is that you are not returning a correct JSON object, you are returning a string, maybe try to change:

From this:

def Detail(request):
    data    = json.loads(request.body.decode("utf-8"))
    orid    = data['orid']
    ornm    = data['ornm']
    print('id :', orid,'nama :', ornm)

    return JsonResponse('Payment complete', safe=False)

to something like this:

def Detail(request):
    data    = json.loads(request.body.decode("utf-8"))
    orid    = data['orid']
    ornm    = data['ornm']
    print('id :', orid,'nama :', ornm)

    return JsonResponse({'status':'Payment complete'}, safe=False)

See for example Django documentation:

django json response

CodePudding user response:

JavaScript

<script>
var id_order = document.getElementsByClassName('id_order')
for (i = 0; i < id_order.length; i  ) {
  id_order[i].addEventListener('click', function(){
        var orid  = this.dataset.product
        var ornm  = this.dataset.act
        console.log('orid :', orid)
        console.log('ornm :', ornm)
        data(orid, ornm)
    })
    
}
function data(orid, ornm){
  console.log('orid :', orid, 'ornm :', ornm)
const url = "Detail"
fetch(url, {
        method :'POST',
        headers : {
            'Content-Type' : 'application/json',
            'X-CSRFToken'  :  csrftoken,
        },
        body:JSON.stringify({'orid':orid, 'ornm':ornm}),
    })
        .then((response) => response.json())
                .then((data) => {
                    console.log('Success:', data);
                    window.location.href = "{% url 'Detail_pem' %}"

                })
}
</script>

view.py

def Detail_pem(request):
    data    = json.loads(request.body.decode("utf-8"))
    orid    = data['orid']
    ornm    = data['ornm']

    print('id :', orid, 'nama :', ornm)

    context = {'orid ':orid , 'ornm':ornm}
    return render(request, 'store/detail.html', context )
    
def Detail(request):
    data    = json.loads(request.body.decode("utf-8"))
    orid    = data['orid']
    ornm    = data['ornm']
    print('id :', orid,'nama :', ornm)

    return JsonResponse('Payment complete', safe=False)

Getting Error like that JSONDecodeError at /Detail_pem Expecting value: line 1 column 1 (char 0) enter image description here

  • Related