vendredi 14 août 2020

Post request but getting error The GET method is not supported for this route. Supported methods: POST Laravel/Javascript

I am sending a POST request from javascript to laravel backend like so:

post(url, 'data=' + JSON.stringify({'app_name': appName, 'business_name': partnerName, 
'source': source, 'http_referrer': referrer, 'link' : link}));

function post(url, data) {
  let http = new XMLHttpRequest();
  http.open('POST', url, true);
  http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  http.send(data);
}

But getting an error: The GET method is not supported for this route. The error appears when i'm passing long url params like:

http://127.0.0.1:7000/affiliates/businessName/Tobi?source=Koala?utm_source=koala-apps-shopify-inspector&utm_medium=koala-apps-shopify-inspector&utm_campaign=koala-apps-shopify-inspector&utm_term=koala-apps-shopify-inspector&utm_content=koala-apps-shopify-inspector

Until this point of url http://127.0.0.1:7000/affiliates/businessName/Tobi?source=Koala?utm_source=koala-apps-shopify-inspector the POST request sends fine, when there is more multiple params getting the following error.

The route for recieving the request is:

Route::post('create-affiliate-opens', 'AffiliatesController@createAffiliateOpens');

Controller method:

 public function createAffiliateOpens(Request $request)
{
    $data = json_decode($request->data, true);
    $user = User::where('business_name', '=', $data['business_name'])- 
>first();
    if (!$user) return abort(404);

    $user->affiliateOpens()->create([
        'ip'            => $request->ip(),
        'http_referrer' => $data['http_referrer'],
        'link'          => $data['link'],
        'app_name'      => $data['app_name'],
        'business_name' => $data['business_name'],
        'source'        => preg_replace("![^a-z0-9]+!i", "-", 
 $data['source']),
        'created_at'    => Carbon::now()->format('Y-m-d H:i'),
    ]);

    return $this->respondCreated([
        'success' => true,
    ]);
}

How could I fix this problem, what am I not seeing?

If something is not clear, please say so, I will provide more information.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire