简体   繁体   English

请求的json解析失败,解析json文档时出错

[英]requested json parse failed ,there was an error parsing the json document

var objectData ={
             "emailAdress" :  document.getElementById('emailAddress').value ,
              "password":     document.getElementById('password').value }

 var objectDataString = JSON.stringify(objectData);
 alert(objectDataString);


   var url = "url";
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    alert('connected..');
    }
  };
xmlhttp.open("POST",url,true);
xmlhttp.send(objectDataString);

      $.ajax({

            type: "POST",
            URL:"login.php",
            contentType:"application/json",
            CrossDomain:true,
            data:  JSON.stringify(objectData),       
            dataType: 'json',
            success: function (data) {

                alert("success");
                var ret = jQuery.parseJSON(data);
                alert(ret);

I have login form which contains userid and password.i have to convert that user entered info into json and send it to the server.but getting json parsed error .unable to find the error.please tell me where im doing wrong..?im trying from so many hours but unable to find . 我有包含用户名和密码的登录表单。我必须将该用户输入的信息转换为json并将其发送到服务器。但是得到json分析错误。无法找到错误。请告诉我我在哪里做错了..尝试了几个小时但找不到。

Ignoring the XHR code, and just looking at the ajax call: 忽略XHR代码,仅查看ajax调用:

  1. There's no CrossDomain option. 没有CrossDomain选项。 There's a crossDomain option, though. 不过,有一个crossDomain选项。 (JavaScript is case-sensitive.) You probably don't want that option at all, though, with the URL you're giving. (JavaScript区分大小写。)但是,您提供的URL可能根本不需要该选项。

  2. You're telling jQuery that the data coming back is JSON. 您正在告诉jQuery,返回的数据是JSON。 That means jQuery will parse it for you before giving you the data argument to your success function. 这意味着jQuery将在为成功函数提供data参数之前为您解析它。 You don't need (or want) to call JSON.parse on it, that's already been done for you. 您不需要(或不想)对其调用JSON.parse ,这已经为您完成。 data will be parsed data. data将被解析为数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM