skip to Main Content

Hello there my website is on cpanel and when i make an api call it returns contnet-type html instead of json note that it worked perfectly on my localhost but for some reason it isn’t working
Code

public function fetch_companies()
{
    //getting company that is linked to coupon
    $companies_id = Coupons::where('company_id' , '!=', 'null')->pluck('company_id');
    $companies = Companies::whereIn('id' , $companies_id)->get();

    return response()->json($companies);
}

i tried setting the headers to json like this

    return response()->json($companies)->withHeaders([
            'Content-Type' => 'application/json',
        ]);

but it didn’t work

here is the link to my website you may test it using postman
http://coupon-app.epizy.com/company/api/fetch

just to put you in the picture the code currently running this page is this

public function fetch_companies()

    {
        //getting company that is linked to coupon
        $companies_id = Coupons::where('company_id' , '!=', 'null')->pluck('company_id');
        $companies = Companies::whereIn('id' , $companies_id)->get();

        return response()->json($companies)->withHeaders([
                'Content-Type' => 'application/json',
            ]);
    }

if you need any information please comment and Thanks in Advance

2

Answers


  1. Chosen as BEST ANSWER

    well i found out that the cause of this problem was my server provider as you can find here https://infinityfree.net/support/javascript-error-using-api-or-mobile-android-app/ it is a security "feature" although it is not a feature that blocks any request that doesn't accept cookies and run javascript


  2. Change Accept & Content-Type to Application/json on the header

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search