سلام
من قصد دارم درخواست زیر رو به یک api بفرستم:
$.ajax({ url: 'https://theapi.com/api/verify', data: {"mobile":"09123456789"}, type: 'POST', contentType: 'application/json;charset=utf-8', dataType: 'json', success: function() { alert("Success"); }, error: function() { alert('Failed!'); }, });
اما خطای CORS میده. ولی وقتی قسمت زیر رو از کد حذف میکنم به سادگی اس ام اس تایید دریافت میشه و اتصال به API به درستی صورت میگیره.
contentType: 'application/json;charset=utf-8',
دلیل و روش رفع مشکل چیه؟ (بدون اینکه اینکه content type حذف بشه)
It turns out that CORS only allows some specific content types. The only allowed values for the Content-Type header are: application/x-www-form-urlencoded multipart/form-data text/plain Source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS To set the content type to be 'application/json', I had to set a custom content type header in the API. Just removed the last header and added this one:
->header('Access-Control-Allow-Headers', 'Content-Type');
and it is working all good.
آیا مایل به ارسال نوتیفیکیشن و اخبار از طرف راکت هستید ؟