0

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
6
  • Can you see in the request that the data is being send? the log you're showing is from console.log(contactForm.serializeArray())? Commented Aug 24, 2018 at 14:11
  • That is the {success: Array(0)} below right? Commented Aug 24, 2018 at 14:20
  • if you return any other value it like "success" => "hello", does it show it? Commented Aug 24, 2018 at 14:21
  • return response()->json(['success'=>"hello"]); returns {success: "hello"} Commented Aug 24, 2018 at 14:24
  • @JohnCroker remove dataType: "json" and add cache: false Commented Aug 25, 2018 at 6:19

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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)
            }
    })

1 Comment

Thanks but made no effect.
0

You can use the below solution to send data:

data : contactForm.serialize()

1 Comment

I have a amended the ajax but $request->all() still sends back an empty array. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.