skip to Main Content

Today I am facing a strange issue that I never faced before. My ajax call is returning response with 200 status but somehow it always trigger error event instead of success. This is what I am doing

$.ajax({
        type: "GET",
        url: "swap.php",
        data: "key=dbdcd39f0f8f5077e7308f3d3d5d8cac&code="+$("#code").val(),
        contentType: 'application/json; charset=utf-8',
        dataType:"json",
        beforeSend: function() {
                $("#msg").html("<img src='imgs/loading_circle.gif'/>");
        },
        success: function(result){
                alert(result);
        },
        error: function(xhr,status,error){
            alert(error.Message); // receiving Undefined message
        }
    });

PHP code of swap.php

<?php
header('Content-Type: application/json');
$resp["data"]="Record updated successfully";
print json_encode($resp);
?>

This is the response which I get when directly put the url in address bar

enter image description here

Below is the screenshot of chrome’s network tab

enter image description here

2

Answers


  1. The response image you’re showing not the actual request, it’s a pre flight check, Check the Request method it’s OPTIONS not the "GET" as what you are trying. it’s just checking for the "CORS" in this request. Not the actual ajax call.

    Check the actual request, just after this. You will find the error.

    Login or Signup to reply.
  2. you can try passing custom headers

    headers: {
                'Content-Type': 'application/json',
                "Accept": 'application/json',
            }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search