我有一个奇怪的错误,没有'Access-Control-Allow-Origin'标题。我知道我必须给予原始域(客户端)一些权限才能访问服务器,并且我可以正常处理其他一些请求。但是当涉及到另一个要调用的函数时,它只会说'No-Control-Control-Allow-Origin'标题。
我的客户端托管于heroku,服务器托管在godaddy ......
当错误显示时,这是函数:
public function store(Request $request)
{
$ffSpending = new FriendsFamilySpending();
$ffSpending->user_id = Auth::user()->id;
$ffSpending->team_id = Auth::user()->student->team_id;
$ffSpending->management_units = $request->input('management_units');
$ffSpending->sales_units = $request->input('sales_units');
$ffSpending->product_units = $request->input('product_units');
$ffSpending->ip_units = $request->input('ip_units');
$ffSpendingRules = array(
'management_units' => 'required',
'sales_units' => 'required',
'product_units' => 'required',
'ip_units' => 'required'
);
$ffSpendingValidator = \Validator::make($ffSpending->getAttributes(), $ffSpendingRules);
if ($ffSpendingValidator->passes()) {
$team = Team::find($ffSpending->team_id);
$currentBalance = $this->teamService->checkBalance($team);
//Baseline costs
$BASELINE = FriendsFamilySpending::BASELINE;
//Get Prices for each unit
$IP_ONE_TIME_COST = OperationalExpensePrice::getPrice(OperationalExpense::IP, $request->input('ip_units'), OperationalExpenseSerie::FF);//one-time IP costs
$MANAGEMENT = OperationalExpensePrice::getPrice(OperationalExpense::MANAGEMENT, $request->input('management_units'), OperationalExpenseSerie::FF);
$SALES = OperationalExpensePrice::getPrice(OperationalExpense::SALES, $request->input('sales_units'), OperationalExpenseSerie::FF);
$PRODUCT = OperationalExpensePrice::getPrice(OperationalExpense::PRODUCT, $request->input('product_units'), OperationalExpenseSerie::FF);
//Monthly Costs
$quartlyCosts = $BASELINE + $MANAGEMENT + $SALES + $PRODUCT;
$newBalance = $currentBalance;
$workingMoney = $newBalance - $IP_ONE_TIME_COST;
$minimumMonth = 12; // the minimum amount of time they must be able to afford (Staging Day = 12 months)
//Calculate how many months they (team) can survive
$survivingMonths = 0;
while ($workingMoney >= $quartlyCosts) {
$workingMoney = $workingMoney - $quartlyCosts; //deduct monthly costs from the current working money
$survivingMonths = $survivingMonths + 3; // quartly spending
if ($survivingMonths > 24) { // team survives the whole staging and deal day
break;
}
}
// month to minute - Conversation ratio
$monthToMinute = 6.25; // (75min / 12month) = 6.25min a month
$totalMinutes = $survivingMonths * $monthToMinute;
$minMinutes = 75; //the minimum amount of time they must be able to afford
//Check if team makes it till the deal day
if ($survivingMonths < $minimumMonth) {
return response()->json(['message' => 'With your current spending plan, you will not make it to deal day. Please try again with less spending plan. Currently you run out of money after ' . $totalMinutes . ' minutes. You have to survive at least 75 minutes."', 'success' => false, 'status' => 500, 'data' => null]);
}
$ffSpendingRes = $this->ffSpendingService->save($ffSpending);
if ($ffSpendingRes) {
$this->ffSpendingService->score(Auth::user()->student->team->class_id); // update ff spending scoring
$this->ffSpendingService->updateTotalScore(Auth::user()->student->team->class_id);
//Update balance
$team = Team::find($ffSpending->team_id);
$this->teamService->updateBalance($team, $workingMoney);
if ($survivingMonths >= $minimumMonth && $survivingMonths < 24) {
$survivingMonthsAfterStagingDay = $survivingMonths - $minimumMonth;
$survivingMonthsAfterStagingDayToMinute = $survivingMonthsAfterStagingDay * $monthToMinute;
$outOfMoney = new OutOfMoney();
$outOfMoney->team_id = Auth::user()->student->team_id;
$outOfMoney->stage = OutOfMoney::$FF_SPENDING;
$outOfMoney->is_running_out_of_money_on_deal_day = 1;
$outOfMoney->month = $survivingMonthsAfterStagingDay;
$outOfMoney->minutes = $survivingMonthsAfterStagingDayToMinute;
$outOfMoney->monthly_cost = $quartlyCosts;
$outOfMoney->save();
} else {
$outOfMoney = new OutOfMoney();
$outOfMoney->team_id = Auth::user()->student->team_id;
$outOfMoney->stage = OutOfMoney::$FF_SPENDING;
$outOfMoney->is_running_out_of_money_on_deal_day = 0;
$outOfMoney->monthly_cost = $quartlyCosts;
$outOfMoney->save();
}
return response()->json(['message' => 'Success', 'success' => true, 'status' => 200, 'data' => $ffSpending]);
} else {
return response()->json(['message' => 'Error', 'success' => false, 'status' => 500, 'data' => null]);
}
} else {
return response()->json(['message' => 'Validation Failed', 'success' => false, 'status' => 400, 'data' => array('class' => $ffSpendingRules)]);
}
注意:如果我更改了函数并使其变得简单,例如返回'test',它就不再显示原始访问错误。 我想知道函数是否有任何问题,为什么不显示实际错误,但它显示cors错误。
有什么建议吗?
答案 0 :(得分:0)
正在发生的是ORIGINS请求即将进入并被拒绝,因为它不在您允许的方法中。
'allowedMethods' => ['GET', 'POST', 'PUT', 'DELETE', 'ORIGINS']
您还希望将allowedOrigins
设置为某种内容。
我总是使用MDN作为参考。