Skip to content
Advertisement

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

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}}" class="btn btn-warning id_order btntam" >Detail</button> </a> -->
              <button data-product="{{order.id}}" data-act="{{order.name}}" class="btn btn-warning id_order btntam" >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 )

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement