Laravel 5-Ajax POST-$ request-> all()在PostController中返回空数组 [英] Laravel 5 - Ajax POST - $request->all() returns empty array in PostController
问题描述
道歉,但我已经尝试过对每个问题的每条建议,甚至与此几乎毫无疑问.
Apologies for repeat but i've tried every suggestion to every question even vaguely similar to this to no end.
ajax帖子:
var contactForm = $('#contactForm');
contactForm.on('submit', (event) => {
event.preventDefault()
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')
}
})
console.log(contactForm.serializeArray())
$.ajax({
dataType: "json",
method : 'POST',
url : 'contact-us/post',
data : contactForm.serializeArray(),
success: (res) => {
console.log(res)
}
})
})
路线处理信息:
Route::post('contact-us/post', 'EnquiryPostController@store');
PostController存储方法:
PostController store method:
public function store(Request $request)
{
return response()->json(['success'=>$request->all()]);
}
更新:
只需要澄清一下:
- 我正在传递CSRF令牌.
- 表单输入具有名称属性.
- PostController正在接收POST请求,但根本不包含任何表单数据.
- 此问题与AJAX POST和常规旧表格POST一起发生
推荐答案
原来,这不是Laravel问题,而是Windows Server/web.config问题.我将使用在此处找到的XML设置我的网站.经过大量的反复试验,使用以下XML解决了该问题:
Turns out this was not a Laravel issue but a Windows Server/web.config issue. I'd setup my site using the XML found here. After much trial and error the issue was solved using this XML:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<ModSecurity enabled="true" configFile="c:\inetpub\wwwroot\modsecurity.conf" />
<httpErrors errorMode="Detailed" />
<modules runAllManagedModulesForAllRequests="true" />
<defaultDocument>
<files>
<clear />
<add value="index.php" />
</files>
</defaultDocument>
<rewrite>
<rules>
<rule name="CanonicalHostNameRule1">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^dev\.financialservicesexpo\.co\.uk$" negate="true" />
</conditions>
<action type="Redirect" url="http://dev.financialservicesexpo.co.uk/{R:1}" />
</rule>
<rule name="default" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/index.php" />
</rule>
</rules>
</rewrite>
<staticContent>
<clientCache cacheControlMaxAge="8.00:00:00" cacheControlMode="UseMaxAge"/>
</staticContent>
</system.webServer>
</configuration>
我希望能够解释为什么此问题得以解决,但是我对服务器配置了解得很少.如果有人想发表有见识的评论,至少我会发现它很有趣.
I wish I could explain why this fixed the issue but I have very little understanding of server configuration. If anyone would like to comment with insight I at least would find it interesting.
如果没有帮助请仔细阅读
要解决这个问题,需要尝试许多不同的解决方案,这些解决方案甚至是在发给StackOverflow的更多问题上找到的,等等.在与同事聊天并大声解决问题后,最终在两分钟内解决了问题.我一直需要学习的一个令人沮丧的课程...因此,希望这对其他人有帮助,以下是您应检查的上述解决方案的列表.
Getting to the bottom of this issue involved trying a lot of different solutions found across even more questions posted to StackOverflow, etc. and was ultimately solved in two minutes after chatting to a colleague and working though the issue out loud. A frustrating lesson I keep needing to learn... So, in the hope this might help others the following is a list of the aforementioned solutions you should check.
- 检查您的表单输入是否具有名称属性.
- 清除浏览器缓存.
- 清除Laravel缓存数据.
- 重新启动网站
- 使用AJAX时,请同时尝试serialize()和serializeArray()
- 使用AJAX时,请尝试使用不同的DataType和ContentType提交.默认值应该很好,但是如果您做不同的事情,则值得大喊.
- CSRF令牌:通过隐藏的输入字段或在请求标头中设置添加令牌.只需要一个.
- 确保/storage和/bootstrap文件夹已启用读/写权限.
如果有人对我在这里写的内容有任何建议或更正,请大声说出来.我绝对不是专家.
If anyone has any suggestions or corrections to what I have written here please do speak up. I am no means a expert.
这篇关于Laravel 5-Ajax POST-$ request-> all()在PostController中返回空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!