Asked  7 Months ago    Answers:  5   Viewed   38 times

When making an ajax call, when contentType is set to application/json instead of the default x-www-form-urlencoded, server side (in PHP) can't get the post parameters.
in the following working example, if I set the contentType to "application/json" in the ajax request, PHP $_POST would be empty. why does this happen? How can I handle a request where contentType is application/json properly in PHP?

    cache: false,
    type: "POST",
    url: "xxx.php",
    //contentType: "application/json",
    processData: true,
    data: {my_params:123},
    success: function(res) {},
    complete: function(XMLHttpRequest, text_status) {}


Wednesday, March 31, 2021
answered 7 Months ago

The problem may be with the server side, since the client seems correct. That long call to usleep() may freeze server output, and if the client doesn't receive the HTTP response status line, it can't even fire the start event on the XHR object (for example the service may respond with a 404 status code, which is an error at the protocol level and is reported as delivering failure).

Also, the browser may need to accumulate a certain number of bytes before passing the event to the JS engine. I used the following script on the server side to send a 4KB load of data N times (4096 octets seems enough for Chrome at least to update the progress counter):

header("Content-Type: text/plain");
header("Content-Length: " . ($size * $times));

$size = 4096;
$times = 5;

function send($size) {
  while($size-- > 0) {
    echo "A";
  echo "n";

for ($i = 0; $i < $times; $i++) {

This is the script on the client side:

        xhr: function() {
                var xhr = new window.XMLHttpRequest();
                xhr.addEventListener("progress", function(evt){
                    if (evt.lengthComputable) {
                }, false);
            return xhr;
        , type: 'post'
        , cache: false
        , url: "sleep.php"});
Wednesday, March 31, 2021
answered 7 Months ago

Don't try to encode the data yourself in the URL. Let jQuery do it for you.

var search = {
   city: 'Santa Monica',
   region: 'CA',
   country: 'US',
   within: 15,
   within_unit: 'M',
   start: '2013-12-10',
   max: 50
var url = ''+$.param(search);

   type: 'GET',
   data: {
      url: url
   dataType: "json",
   success: jsonFunction,
   error: jsonFunction
Saturday, May 29, 2021
answered 5 Months ago

The issue is that the JSONP response is being caught by the actual page, outside of the sandboxed JavaScript code that the Chrome content script limits you too.

jQuery17105683612572029233_1323808231542 is the name of the callback function that the jQuery JSONP call has dynamically generated for the specific call. This function is being defined in the sandboxed area the content script has access to.

The only workaround that I am aware of, which worked for me, is to make an XHR call from the content script. As of Chrome 13 you can make XHR calls cross-domain from the content scripts (pretty cool). In your manifest file you need to add the external URL to the permissions:

    "permissions": [

You can then make the XHR call like so:

var xhr = new XMLHttpRequest();"GET", "", true);
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
       //handle the xhr response here

You will need to do some of the things that jQuery was doing automatically for you, like encoding the values of the data object into the XHR URL (in your case the "imrUrl" and "returnString") as well as convert the response from the xhr.responeText or xhr.reponseXML into an object.

The downside of this approach is that if you are sharing this code between a Chrome extension and something else (like a bookmarklet) you now have to have different logic for the Chrome use case.

For more info see: Chrome Extension XHR

Thursday, June 24, 2021
answered 4 Months ago

The code gets the data from nseindia site which comes as a JSON string in responseDiv element.

Required References

enter image description here

3 Class Module i have used

  • cJSONScript
  • cStringBuilder
  • JSON

(I have picked these class modules from here)

You may download the file from this link

Standard Module

Const URl As String = ""
Sub xmlHttp()

    Dim xmlHttp As Object
    Set xmlHttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
    xmlHttp.Open "GET", URl & "&rnd=" & WorksheetFunction.RandBetween(1, 99), False
    xmlHttp.setRequestHeader "Content-Type", "text/xml"

    Dim html As MSHTML.HTMLDocument
    Set html = New MSHTML.HTMLDocument
    html.body.innerHTML = xmlHttp.ResponseText

    Dim divData As Object
    Set divData = html.getElementById("responseDiv")
    ' Here you will get a string which is a JSON data

    Dim strDiv As String, startVal As Long, endVal As Long
    strDiv = divData.innerHTML
    startVal = InStr(1, strDiv, "data", vbTextCompare)
    endVal = InStr(startVal, strDiv, "]", vbTextCompare)
    strDiv = "{" & Mid(strDiv, startVal - 1, (endVal - startVal) + 2) & "}"

    Dim JSON As New JSON

    Dim p As Object
    Set p = JSON.parse(strDiv)

    i = 1
    For Each item In p("data")(1)
       Cells(i, 1) = item
       Cells(i, 2) = p("data")(1)(item)
        i = i + 1

 End Sub
Wednesday, June 30, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :