问题
Apologies for repeat but i've tried every suggestion to every question even vaguely similar to this to no end.
ajax post:
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 handling post:
Route::post('contact-us/post', 'EnquiryPostController@store');
PostController store method:
public function store(Request $request)
{
return response()->json(['success'=>$request->all()]);
}
Console output
response header from network tab
request header from network tab
from data from network tab
UPDATE:
Just to clarify:
- I am passing the CSRF token.
- The form inputs have name attributes.
- The PostController is receiving the POST request but simply does not contain any form data.
- This issue happens with AJAX POST and regular old form POST
回答1:
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.
IF THIS DID NOT HELP PLEASE READ BELOW
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.
- Check your form inputs have name attributes.
- Clear your browser cache.
- Clear Laravel cache data.
- Restart website
- When using AJAX try both serialize() and serializeArray()
- When using AJAX try submitting with different DataTypes and ContentTypes. The default should be fine but worth a shout if your doing something different.
- CSRF token: Add it via either a hidden input field or set in the request header. Only one should be needed.
- Make sure the /storage and /bootstrap folders have read/write privileges enabled.
If anyone has any suggestions or corrections to what I have written here please do speak up. I am no means a expert.
回答2:
Try in your ajax request using type
instead of method
like
$.ajax({
dataType: "json",
type : 'POST',
url : 'contact-us/post',
data : contactForm.serializeArray(),
success: (res) => {
console.log(res)
}
})
回答3:
You can use the below solution to send data:
data : contactForm.serialize()
来源:https://stackoverflow.com/questions/52006251/laravel-5-ajax-post-request-all-returns-empty-array-in-postcontroller