Default Content type on Ajax function

The built in Ajax function is doing a content type of application/x-www-form-urlencoding which is technically incorrect. My web server is very strict on this and therefore I have had to create my own ajax function (using the Options feature is too difficult).

I think the default content type should be application/json or at a minimum if you pass in a body/content it should switch to application/json.

AppStudio’s Ajax function is simply a wrapper for the widely used jQuery Ajax function:

https://api.jquery.com/jquery.ajax/

Here is what the docs say about this:

contentType (default: ‘application/x-www-form-urlencoded; charset=UTF-8’)
Type: Boolean or String
When sending data to the server, use this content type. Default is “application/x-www-form-urlencoded; charset=UTF-8”, which is fine for most cases. If you explicitly pass in a content-type to $.ajax(), then it is always sent to the server (even if no data is sent). As of jQuery 1.6 you can pass false to tell jQuery to not set any content type header. Note: The W3C XMLHttpRequest specification dictates that the charset is always UTF-8; specifying another charset will not force the browser to change the encoding. Note: For cross-domain requests, setting the content type to anything other than application/x-www-form-urlencoded, multipart/form-data, or text/plain will trigger the browser to send a preflight OPTIONS request to the server.

I too am having problems with Ajax php and my new web server (old server company taken over by new company).
I receive an error when issuing the command Ajax(“ajaxGetGloURL.php/?urlToGet=” & encodeURIComponent(myurl)) where myurl="http://www.jumpscore.com.au/63765DrawGeo.txt”

The error is:

"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /63765DrawGeo.txt
on this server.<br />
</p>
<p>Additionally, a 403 Forbidden
error was encountered while trying to use an ErrorDocument to handle the request.</p>
</body></html>

I have changed permissions for folders and files but still doesn’t work. Everything worked with the old server. PHP version and settings are the same as old server. New server support don’t seem to know what is wrong.

Any help/suggestions with this would be greatly appreciated.

Maybe you need set some other ajax options.
Here some sample code to set ajax options

  JavaScript 
  var ajaxOptions = {
  "headers" : {"Authorization":  token},
  "contentType" : "application/json",
  "dataType": "json",
  "complete": completeCallback,
  "type": "GET"
  }
  End JavaScript

  req=Ajax(url, ajaxOptions)

It’s understandable that you’ve had to create your own Ajax function to ensure the correct content type is being sent to your server. While the default content type of application/x-www-form-urlencoding may work for some cases, it’s not always the most appropriate choice.

Using application/json as the default content type or switching to it automatically when a body/content is passed in would definitely be a more modern and widely accepted approach. JSON has become the de facto standard for data exchange on the web, and most modern web servers and APIs support it out of the box.

If you’re concerned about compatibility with older servers or APIs that may not support JSON, you could consider adding an optional parameter to your Ajax function to allow users to specify the content type they want to use. That way, users could still use your function with older servers that require a different content type, while also allowing them to take advantage of the benefits of JSON where possible :nerd_face: :nerd_face:

Overall, it’s great that you’re taking the time to ensure that your Ajax function is sending the correct content type to your server. By doing so, you’ll be helping to ensure that your web application is compatible with a wide range of servers and APIs, and that your users are getting the best possible experience.