Filipe Lopes Filipe Lopes - 4 months ago 66
Ajax Question

Laravel PHP 403 Error

Ok so, I'm using Laravel to develop a platform.
I have a form that allows the user to register a domain. This form is submited through ajax to "[my domain]/thedomains/create". Here is the code that does so:

$("#btn_doms_create_submit").click(function (){
$.ajax({
type: 'GET',
url: "/thedomains/create",
data: { _token : $("input[name=\"_token\"]").val(),
domain : $("input[name=\"create_dom\"]").val(),
field_ip : $("input[name=\"create_ip\"]").val(),
obs : $("textarea[name=\"create_obs\"]").val(),
active : $("input[name=\"create_active\"]").is(":checked")+"",
itld : $("input[name=\"create_itld\"]").is(":checked")+"" },
dataType: 'json',//returned data
success: function(data){
if (data["errors"] == "")
{
toastr.success(data.message);
$("#doms_create").modal("hide");
updateTable();
}
else
{
var txtError = "";
for (error in data.errors) {
txtError += "<li>" + data.errors[error] + "</li>";
}
toastr.warning(data.message + "<ul>" + txtError + "</ul>");

if (data.errors.domain != null) {
$("#div_create_dom").attr("class","form-group has-error");
}
if (data.errors.field_ip != null) {
$("#div_create_ip").attr("class","form-group has-error");
}
}
}
});
});


The code behind "/thedomains/create" is

public function create(Request $request) {
$return_val = [ "message" => trans("system.dom_succ", ["name" => $request->dom]),
"errors" => "" ];

$validator = Validator::make($request->all(), [
"domain" => "required|max:255|url",
"field_ip" => "max:255"
],[
"domain.required" => trans("system.val_dom_required"),
"domain.max" => trans("system.val_dom_max"),
"domain.url" => trans("system.val_dom_url")
]);

if (!$validator->fails()) {
$dom = new Doms;

$dom->name = $request->domain;
$dom->ip = $request->ip;
$dom->obs = $request->obs;

if ($request->active == "true")
$dom->status = true;
else
$dom->status = false;

if ($request->itld == "true")
$dom->is_top_level_domain = true;
else
$dom->is_top_level_domain = false;
} else {
$return_val["message"] = trans("system.note_errors");
$return_val["errors"] = $validator->errors();
}

return json_encode($return_val);
}


As you can see, I have a validation to verify that all the required data is present and valid, according to the needed standards.
The problem is: When I submit this form with some invalid domain or with no domain at all, the verification occurs naturally, the return is given, etc. But when I have a valid domain, the validation throughs the following error on my browser's (chrome) console:


GET [my domain]/thedomains/create?_token=2vsOrtkcWGid5Ex2HegtY3Fw2E…aE&domain=http%3A%2F%2Ftecnosece.com&field_ip=&obs=&active=true&itld=false 403 (Forbidden)


Idk why this happens. I've already changed the route from "domains" to "thedomains", thinking there could be some kind of blocking from my host, but that didn't solve the problem.
Any idea of what's happening?




Edit:

As requested, here is my routes portion for "/thedomains":

Route::group(['prefix' => 'thedomains'], function () {
Route::get("/",function () {
return view("doms");
});

Route::get("/all", "DomsController@getAll");

Route::post("/get", "DomsController@get");

Route::get("/create", "DomsController@create");

Route::post("/edit", "DomsController@edit");

Route::post("/del", "DomsController@del");

Route::get("/search", "DomsController@search");

Route::post("/migrate", "DomsController@migrate");
});

Answer

Are you using GET instead of POST on purpose? Also, can you share your entries in routes.php for '/thedomains/create' ?

Normally, you'd want to POST that data to create a new resource.