18846

XMLHTTPrequest sending empty post

Question:

I've done a lot of times before, so I'm honestly confused why this is failing to pass anything. I've tried printing results (script gets a response and prints the file).

function submit_form_inpage(path, data, watchForChange){ alert(data); watchForChange = watchForChange || false; var request = new XMLHttpRequest(); request.open('POST', path, true); request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); if (watchForChange == true) { request.onreadystatechange = function () { if (request.readyState == 4) { document.write(request); if (request.status==200 && request.status < 400){ var xmldata=request.responseText //retrieve result as an XML object alert("XML:" + xmldata); } else{ alert("An error has occured making the request:" + request.status ); } } } } var temp_string = array_to_string_for_post(data); var temp = JSON.stringify(data); alert(temp); request.send(temp); }

My php is

print_r($_POST);

and my result is

XML: Array ()

Despite the fact that data passed in (which is double-checked right before being sent by my alert) is

{"reason":"get_stuff","build_name":"test"}

Answer1:

You said you were sending form encoded data.

<blockquote> request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); </blockquote>

Then you sent:

<blockquote> temp = JSON.stringify(data); </blockquote>

JSON is application/json not application/x-www-form-urlencoded (and isn't natively supported by PHP anyway).

Either encode your data as application/x-www-form-urlencoded or correct your content-type and <a href="https://stackoverflow.com/a/19007851/19068" rel="nofollow">parse it manually in PHP</a>.

Recommend

  • Magic number for plain text file
  • AJAX Permission Denied On IE?
  • parse json list in reverse order
  • Will these ActiveXObject and XMLHttpRequest checks apply for any other browser than IE6?
  • Returning Json from controller, never a success
  • Api Gateway cannot allow Access-Control-Allow-Origin
  • Problem in Loading xml from specified url using javascript in FF & Google Chrome
  • passing a javascript variable to PHP with xmlhttprequest
  • Bad request using file_get_contents for PUT request in PHP
  • Optimizing database types to compact database (SQLite)
  • C# - Serializing and deserializing static member
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Cross-Platform Protobuf Serialization
  • Sending data from AppleScript to FileMaker records
  • Can I make an Android app that runs a web view in Chrome 39?
  • Symfony2: How to get request parameter
  • ORA-29908: missing primary invocation for ancillary operator
  • Do I've to free mysql result after storing it?
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • How to disable jQuery.jplayer autoplay?
  • How to include full .NET prerequisite for Wix Burn installer
  • log4net write single file for each call to log.info
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Free memory of cv::Mat loaded using FileStorage API
  • Getting error when using KSoap library to consume .NET web services
  • How do I configure my settings file to work with unit tests?
  • How to stop GridView from loading again when I press back button?
  • Turn off referential integrity in Derby? is it possible?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster
  • Bitwise OR returns boolean when one of operands is nil
  • Is it possible to post an object from jquery to bottle.py?
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?