How do I encode JSON in PHP via jQuery Ajax post data?

前端 未结 5 804
南旧
南旧 2021-01-31 00:16

I have an HTML form and send data to php file when hitting submit button.

$.ajax({
    url: \"text.php\",
    type: \"POST\",
    data: {
        amount: amount,         


        
5条回答
  •  不知归路
    2021-01-31 01:10

    Your object is most likely being passed properly. It's the way you're capturing the result that returns [object object] as @Spudley explained. The console doesn't know how to display the construct but you can display specific attributes using the object.attribute syntax. Use console.log() on the JS side or the code below for a prettified output.

    // Indent with tabs
    // Data is the parameter sent to the success function in the ajax handler
    JSON.stringify( data , null, '\t');
    

    From How can I pretty-print JSON in (unix) shell script?

    Also Temporarily remove dataType on the ajax handler if you sense there's a bug somewhere. Getting the ouput to show on a GET request should do. Change this back to POST for any operation that modifies something like a database delete or alter.

    Lastly, modify the header as shown in @GroovyCarrot's answer. If you're using Chrome the extra whitespace seems to be a bug: Tab and pre wrapped around JSON output in Chrome

提交回复
热议问题