我在类的函数方法外部声明了一个变量。 我通过发送ajax请求更改了值。要求很好。 但是当我回显变量名时,它仍然返回null,并且完全没有改变。我该如何正确更改?
这是我的代码。
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use DB;
use App\Helpers\globalFunction;
class registerController extends Controller
{
public $documentType;
public $trackingNumber;
public $recipient;
public $description;
public $myvalue = 'hello';
public function pendingDataBeforeSave(Request $request)
{
$this->documentType = $request->documentType;
$this->trackingNumber = $request->trackingNumber;
$this->recipient = $request->recipient;
$this->description = $request->description;
$this->myvalue = 'new one';
}
public function storeSend(Request $request)
{
$office = $request->office;
$sendTo = $request->sendTo;
$status = $request->status;
$message = $request->message;
echo json_encode($this->documentType);
}
}
这里是函数storeSend()的客户端
var object = {
office: officeDropdown.value,
sendTo: senderDropdown.value,
status: statusDropdown.value,
message: message.value
};
var xhr = new XMLHttpRequest();
xhr.open("POST", "/sendData");
xhr.setRequestHeader("Content-type", "application/json");
xhr.setRequestHeader("X-CSRF-TOKEN", document.head.querySelector("[name='csrf-token']").content);
xhr.send(JSON.stringify(object));
xhr.onload = function () {
if (xhr.status == 200) {
alert(xhr.responseText);
} else {
alert(xhr.responseText);
}
};
以及此处的函数pendingDataBeforeSave的客户端代码
var xhr = new XMLHttpRequest();
var object = {
documentType: documentType.value,
trackingNumber: trackingNumber.value,
recipient: recipient.value,
description: description.value
};
xhr.open("POST", "/pendingDataBeforeSave");
xhr.setRequestHeader("Content-type", "text/plain");
xhr.setRequestHeader("X-CSRF-TOKEN", document.head.querySelector("[name='csrf-token']").content);
xhr.send(JSON.stringify(object));
xhr.onload = function () {
if (xhr.status == 200) {
alert('sent');
} else {
alert(xhr.responseText);
}
};
答案 0 :(得分:0)
不需要整个类,因为每次调用都会启动该类,并且在调用以下Route::post('/pendingDataBeforeSave', 'registerController@pendingDataBeforeSave'
时,不会保留Route::post('/sendData', 'registerController@storeSend');
中先前设置的值。 / p>
除非有一种机制可以将值存储在不同的请求中,否则在这里没有提到?
答案 1 :(得分:0)
从它的角度来看,这两种方法是通过两条不同的路线处理的。 PHP的工作方式是,第一个请求完成后,它将不会在多个请求中持久保存属性值。