【问题标题】:AJAX call with post method使用 post 方法的 AJAX 调用
【发布时间】:2012-12-02 01:46:57
【问题描述】:

我对 ajax 和 Web 服务有点陌生。我正在尝试对 post 方法进行 ajax 调用。但我遇到了以下错误。

客户端发送的请求语法错误().</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.25</h3></body></html>

但 GET 方法运行良好。这是我尝试过的代码。

 $.ajax({
                type: "POST", //GET or POST or PUT or DELETE verb
                url: "http://88.80.223.163:8080/lumiin-service/lumiin/control/eventmap/add", // Location of the service
                data: "{"id":50,"isDeleted":"N","ownerId":{"id":32,"fullName":"mailAss,mailAss","status":1,"isDeleted":null,"passWord":"chandra20@gmail.com","abteilung":{"id":86,"isDeleted":null,"domainId":0,"parentId":85,"code":"mailCa","description":null},"authenticatedAccess":-1,"emailId":"chandra20@gmail.com1","uuid":"5988371662040","userCode":"mailAss","localOrg":null,"industryTypeId":{"name":"INSR","id":1,"isDeleted":"N","description":"Insurance"},"memeber":false,"profileType":"CA","lastName":"mailAss","firstName":"mailAss"},"prospectId":{"id":158,"isDeleted":"N","yearofBirth":null,"annualRevenue":0,"nationalityId":{"name":"CH","id":1,"isDeleted":"N","description":"Switzerland"},"titleId":{"name":"Mr","id":1,"isDeleted":"N","description":"Mr"},"maritalstatusId":null,"residencyId":null,"languageId":null,"priorityId":{"name":"High","id":2,"isDeleted":"N","description":"High"},"genderId":null,"abteilung":{"id":86,"isDeleted":null,"domainId":0,"parentId":85,"code":"mailCa","description":null},"emailId":"skmvilla@gmail.com","ownerId":{"id":31,"fullName":"mailCa,mailCa","status":0,"isDeleted":"N","passWord":"mailCa","abteilung":{"id":86,"isDeleted":null,"domainId":0,"parentId":85,"code":"mailCa","description":null},"authenticatedAccess":-1,"emailId":"chandra20@gmail.com1","uuid":null,"userCode":"mailCa","localOrg":null,"industryTypeId":{"name":"INSR","id":1,"isDeleted":"N","description":"Insurance"},"memeber":false,"profileType":"Manager","lastName":"mailCa","firstName":"mailCa"},"company":"company","statusId":{"name":"New","id":1,"isDeleted":"N","description":"New"},"startDate":null,"endDate":null,"lastName":"Search","firstName":"Search"},"eventId":{"URL":null,"location":null,"url":null,"id":92,"type":{"name":"govinda","id":20,"isDeleted":"N","description":"desc"},"isDeleted":"N","followUp":null,"countryId":null,"budget":0,"abteilung":{"id":86,"isDeleted":null,"domainId":0,"parentId":85,"code":"mailCa","description":null},"startDate":1337731200000,"endDate":1338422400000,"prospectId":null,"person":null,"eventName":"eeee-updated","place":null,"description":null}}", //Data sent to server                         
               //contentType: "application/json", // content type sent to server
                dataType: "json", //Expected data format from server
                processdata: true, //True or False
                success: function (json) {//On Successfull service call
                    //shell.loaded();
                    alert("cross domain ajax success full.");

                    var result = json.id;
                    alert("result===" + result);
                    $("#dvAjax").html(result);
                },
                error: ServiceFailed// When Service call fails
            });

如果有人知道,请告知。提前致谢。

问候 卡西克

【问题讨论】:

  • 跨域ajax调用可以使用JSONP。这是一个参考。 stackoverflow.com/questions/5141531/…
  • JavaScript 代码甚至不应该按照编写的方式运行,数据行应该会抛出错误。

标签: javascript jquery web-services


【解决方案1】:

data 属性周围的引号有误。你应该只发送一个简单的 JS 对象,例如

data: {"id": 50, "isDeleted": "N", ... },
dataType: 'json',
etc

【讨论】:

  • 嗨,Phil,我已经尝试了您的上述几点,但是我收到了错误,因为 ReferenceError: error is not defined [Break On This Error] error(err);
【解决方案2】:

尝试对发送的数据使用 JSON.stringify

$.ajax({
    type: "POST", //GET or POST or PUT or DELETE verb 
    url: "http://88.80.223.163:8080/lumiin-service/lumiin/control/eventmap/add", // Location of the service 
    data: JSON.stringify({
        "id": 50,
        "isDeleted": "N",
        "ownerId": {
            "id": 32,
            "fullName": "mailAss,mailAss",
            "status": 1,
            "isDeleted": null,
            "passWord": "chandra20@gmail.com",
            "abteilung": {
                "id": 86,
                "isDeleted": null,
                "domainId": 0,
                "parentId": 85,
                "code": "mailCa",
                "description": null
            },
            "authenticatedAccess": -1,
            "emailId": "chandra20@gmail.com1",
            "uuid": "5988371662040",
            "userCode": "mailAss",
            "localOrg": null,
            "industryTypeId": {
                "name": "INSR",
                "id": 1,
                "isDeleted": "N",
                "description": "Insurance"
            },
            "memeber": false,
            "profileType": "CA",
            "lastName": "mailAss",
            "firstName": "mailAss"
        },
        "prospectId": {
            "id": 158,
            "isDeleted": "N",
            "yearofBirth": null,
            "annualRevenue": 0,
            "nationalityId": {
                "name": "CH",
                "id": 1,
                "isDeleted": "N",
                "description": "Switzerland"
            },
            "titleId": {
                "name": "Mr",
                "id": 1,
                "isDeleted": "N",
                "description": "Mr"
            },
            "maritalstatusId": null,
            "residencyId": null,
            "languageId": null,
            "priorityId": {
                "name": "High",
                "id": 2,
                "isDeleted": "N",
                "description": "High"
            },
            "genderId": null,
            "abteilung": {
                "id": 86,
                "isDeleted": null,
                "domainId": 0,
                "parentId": 85,
                "code": "mailCa",
                "description": null
            },
            "emailId": "skmvilla@gmail.com",
            "ownerId": {
                "id": 31,
                "fullName": "mailCa,mailCa",
                "status": 0,
                "isDeleted": "N",
                "passWord": "mailCa",
                "abteilung": {
                    "id": 86,
                    "isDeleted": null,
                    "domainId": 0,
                    "parentId": 85,
                    "code": "mailCa",
                    "description": null
                },
                "authenticatedAccess": -1,
                "emailId": "chandra20@gmail.com1",
                "uuid": null,
                "userCode": "mailCa",
                "localOrg": null,
                "industryTypeId": {
                    "name": "INSR",
                    "id": 1,
                    "isDeleted": "N",
                    "description": "Insurance"
                },
                "memeber": false,
                "profileType": "Manager",
                "lastName": "mailCa",
                "firstName": "mailCa"
            },
            "company": "company",
            "statusId": {
                "name": "New",
                "id": 1,
                "isDeleted": "N",
                "description": "New"
            },
            "startDate": null,
            "endDate": null,
            "lastName": "Search",
            "firstName": "Search"
        },
        "eventId": {
            "URL": null,
            "location": null,
            "url": null,
            "id": 92,
            "type": {
                "name": "govinda",
                "id": 20,
                "isDeleted": "N",
                "description": "desc"
            },
            "isDeleted": "N",
            "followUp": null,
            "countryId": null,
            "budget": 0,
            "abteilung": {
                "id": 86,
                "isDeleted": null,
                "domainId": 0,
                "parentId": 85,
                "code": "mailCa",
                "description": null
            },
            "startDate": 1337731200000,
            "endDate": 1338422400000,
            "prospectId": null,
            "person": null,
            "eventName": "eeee-updated",
            "place": null,
            "description": null
        }
    }), //Data sent to server
    contentType: "application/json", // content type sent to server 
    dataType: "json", //Expected data format from server 
    processdata: true,  //True or False 

    success: function(json) { //On Successfull service call 
        //shell.loaded(); 
        // alert("cross domain ajax success full.");

        var result = json.id;
        alert("result===" + result);
        // $("#dvAjax").html(result);
    },
    error: ServiceFailed // When Service call fails
});

【讨论】:

    【解决方案3】:

    正如服务器所说,JSON 的“格式”不正确。例如,

     data: "{"id":50,"isDeleted":"N"
    

    ... { 之前的 " 不应该在那里。(可能还有其他语法错误。)

    您可以使用 JSON 验证器,例如http://jsonlint.com/,测试您的 JSON 有效负载。这将为您提供有关特定解析错误的更多详细信息。

    【讨论】:

      【解决方案4】:

      您需要在数据中放置正确的单引号/双引号:

      var yourData = {
      'id': 50,
      'isDeleted': 'N',
      'ownerId': {
          'id': 32,
          'fullName': 'mailAss,mailAss',
          'status': 1,
          'isDeleted': null,
          'passWord': 'chandra20@gmail.com',
          'abteilung': {
              'id': 86,
              'isDeleted': null,
              'domainId': 0,
              'parentId': 85,
              'code': 'mailCa',
              'description': null
          },
          'authenticatedAccess': -1,
          'emailId': 'chandra20@gmail.com1',
          'uuid': '5988371662040',
          'userCode': 'mailAss',
          'localOrg': null,
          'industryTypeId': {
              'name': 'INSR',
              'id': 1,
              'isDeleted': 'N',
              'description': 'Insurance'
          },
          'memeber': false,
          'profileType': 'CA',
          'lastName': 'mailAss',
          'firstName': 'mailAss'
      },
      'prospectId': {
          'id': 158,
          'isDeleted': 'N',
          'yearofBirth': null,
          'annualRevenue': 0,
          'nationalityId': {
              'name': 'CH',
              'id': 1,
              'isDeleted': 'N',
              'description': 'Switzerland'
          },
          'titleId': {
              'name': 'Mr',
              'id': 1,
              'isDeleted': 'N',
              'description': 'Mr'
          },
          'maritalstatusId': null,
          'residencyId': null,
          'languageId': null,
          'priorityId': {
              'name': 'High',
              'id': 2,
              'isDeleted': 'N',
              'description': 'High'
          },
          'genderId': null,
          'abteilung': {
              'id': 86,
              'isDeleted': null,
              'domainId': 0,
              'parentId': 85,
              'code': 'mailCa',
              'description': null
          },
          'emailId': 'skmvilla@gmail.com',
          'ownerId': {
              'id': 31,
              'fullName': 'mailCa,mailCa',
              'status': 0,
              'isDeleted': 'N',
              'passWord': 'mailCa',
              'abteilung': {
                  'id': 86,
                  'isDeleted': null,
                  'domainId': 0,
                  'parentId': 85,
                  'code': 'mailCa',
                  'description': null
              },
              'authenticatedAccess': -1,
              'emailId': 'chandra20@gmail.com1',
              'uuid': null,
              'userCode': 'mailCa',
              'localOrg': null,
              'industryTypeId': {
                  'name': 'INSR',
                  'id': 1,
                  'isDeleted': 'N',
                  'description': 'Insurance'
              },
              'memeber': false,
              'profileType': 'Manager',
              'lastName': 'mailCa',
              'firstName': 'mailCa'
          },
          'company': 'company',
          'statusId': {
              'name': 'New',
              'id': 1,
              'isDeleted': 'N',
              'description': 'New'
          },
          'startDate': null,
          'endDate': null,
          'lastName': 'Search',
          'firstName': 'Search'
      },
      'eventId': {
          'URL': null,
          'location': null,
          'url': null,
          'id': 92,
          'type': {
              'name': 'govinda',
              'id': 20,
              'isDeleted': 'N',
              'description': 'desc'
          },
          'isDeleted': 'N',
          'followUp': null,
          'countryId': null,
          'budget': 0,
          'abteilung': {
              'id': 86,
              'isDeleted': null,
              'domainId': 0,
              'parentId': 85,
              'code': 'mailCa',
              'description': null
          },
          'startDate': 1337731200000,
          'endDate': 1338422400000,
          'prospectId': null,
          'person': null,
          'eventName': 'eeee-updated',
          'place': null,
          'description': null
      }
      };
      

      然后使用:

      ...
      data: yourData,
      contentType: "application/json"
      ...
      

      【讨论】:

        猜你喜欢
        • 2016-11-24
        • 1970-01-01
        • 2023-03-25
        • 1970-01-01
        • 2015-05-19
        • 1970-01-01
        • 2013-01-02
        • 1970-01-01
        • 2012-03-05
        相关资源
        最近更新 更多