javascript - Send JSON with AJAX - Stack Overflow

Is there anything special I have to do to a JSON object before I send it with AJAX? My code looks like

Is there anything special I have to do to a JSON object before I send it with AJAX? My code looks like this:

runAjax(JSON.stringify(data));

}

function runAjax(JSONstring)
{
    ajax = getHTTPObject();
    var params = "?data=" + JSONstring;
    ajax.open("POST", "createtrip.php", true);
    ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajax.setRequestHeader("Content-length", params.length);
    ajax.setRequestHeader("Connection", "close");
    ajax.onreadystatechange = serverSpeaks;
    ajax.send(params);
}

Right now the server is not receiving the data. I get null on the server side but the client side JSONString is set. Is there something I'm doing wrong?

Is there anything special I have to do to a JSON object before I send it with AJAX? My code looks like this:

runAjax(JSON.stringify(data));

}

function runAjax(JSONstring)
{
    ajax = getHTTPObject();
    var params = "?data=" + JSONstring;
    ajax.open("POST", "createtrip.php", true);
    ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajax.setRequestHeader("Content-length", params.length);
    ajax.setRequestHeader("Connection", "close");
    ajax.onreadystatechange = serverSpeaks;
    ajax.send(params);
}

Right now the server is not receiving the data. I get null on the server side but the client side JSONString is set. Is there something I'm doing wrong?

Share Improve this question asked Aug 27, 2009 at 17:00 DanDan 8171 gold badge8 silver badges15 bronze badges 1
  • you can check with Firebug if you have an error or call to server – Eldar Djafarov Commented Aug 27, 2009 at 17:12
Add a ment  | 

3 Answers 3

Reset to default 6

You are sending data over POST, you don't need the '?' character at the beginning of the params variable, also I remend you to encode the JSONString to avoid problems.

Note that you are missing the var statement for the ajax variable, this is declaring it globally (window.ajax) and I think that you don't need it globally...

function runAjax(JSONstring) {
  var params = "data=" + encodeURIComponent(JSONstring), 
      ajax = getHTTPObject();

  ajax.open("POST", "createtrip.php", true);
  ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  ajax.setRequestHeader("Content-length", params.length);
  ajax.setRequestHeader("Connection", "close");
  ajax.onreadystatechange = serverSpeaks;
  ajax.send(params);
}

You should pass through an encoder to send the data correctly. Of course you would first have to see that the variable "data" is well formed as "JSON".

lib to encode/decode

other link encode/decode

The server can deal with post body like name1=value&name2=value2.

If you are using PHP, you can receive the json string by:

$data = file_get_contents("php://input");

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743729370a4497170.html

相关推荐

  • javascript - Send JSON with AJAX - Stack Overflow

    Is there anything special I have to do to a JSON object before I send it with AJAX? My code looks like

    6天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信