如果数据参数包含 - (hiphen)符号,如何处理?

时间:2015-11-17 13:08:27

标签: javascript jquery ajax xmlhttprequest

我尝试使用ajax调用(POST)发送http请求& params和setheaders。参数包括一个特殊字符" - "。
我面临以下错误: -

 callCount:"1",
 page:"/portalcareer?_s.crb=kyJ6d%252fC9jtNjs4BrI7xhqEHgCfU%253d",
 scriptSessionId:"80A8BD291A8E635A37D57F13E5D1F423898",
 c0-scriptName:"picklistControllerProxy",
 c0-methodName:"getChildPickLists",
 c0-id:"0",
 c0-param0:"string:%7B0%20%3A%20341%7D",
 c0-param1:"string:school",
 batchId:"11"

我在

收到错误
  

C 0 SCRIPTNAME:" picklistControllerProxy",

Error at - charcter

Clicked on that and verified its happening exactly there only

这是我的代码

  $("#typeOfSchool").change(function () {
         jQuery.ajax({
                type: 'POST',
                beforeSend: function (abc)
                {
                   abc.setRequestHeader("X-Ajax-Token","ASfafqXVKiGWRh9fBWaJ4XlYI%2fY%3d");
                   abc.setRequestHeader("Accept", "*/*");
                   abc.setRequestHeader("Accept-Encoding","gzip, deflate");
                   abc.setRequestHeader("Accept-Language","en-US,en;q=0.8");
                   abc.setRequestHeader("Connection","keep-alive");
                   abc.setRequestHeader("Content-Length","292");
                   abc.setRequestHeader("Content-Type","text/plain");
                   abc.setRequestHeader("Host","career4.successfactors.com");
                   abc.setRequestHeader("User-Agent","Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36");
                },
                 data: {
                    callCount:"1",
                    page:"/portalcareer?_s.crb=kyJ6d%252fC9jtNjs4BrI7xhqEHgCfU%253d",
                    scriptSessionId:"80A8BD291A8E635A37D57F13E5D1F423898",
                    c0-scriptName:"picklistControllerProxy",
                    c0-methodName:"getChildPickLists",
                    c0-id:"0",
                    c0-param0:"string:%7B0%20%3A%20341%7D",
                    c0-param1:"string:school",
                    batchId:"11"
                },
                 url: 'https://careermela.xyz.com/xi/ajax/remoting/call/plaincall/picklistControllerProxy.getChildPickLists.dwr',
                 success: function(data){
                    alert("process success");
                    alert(data);
                 console.log('succes: '+data);
                  }
               });
    });

1 个答案:

答案 0 :(得分:2)

为数据添加引号

data: {
    "callCount":"1",
    "page":"/portalcareer?_s.crb=kyJ6d%252fC9jtNjs4BrI7xhqEHgCfU%253d",
    "scriptSessionId":"80A8BD291A8E635A37D57F13E5D1F423898",
    "c0-scriptName":"picklistControllerProxy",
    "c0-methodName":"getChildPickLists",
    "c0-id":"0",
    "c0-param0":"string:%7B0%20%3A%20341%7D",
    "c0-param1":"string:school",
    "batchId":"11"
}

如果不起作用,请将内容类型添加到您的ajax调用并对数据进行字符串化。

contentType: 'application/json; charset=utf-8'

var data = {
                "callCount":"1",
                "page":"/portalcareer?_s.crb=kyJ6d%252fC9jtNjs4BrI7xhqEHgCfU%253d",
                "scriptSessionId":"80A8BD291A8E635A37D57F13E5D1F423898",
                "c0-scriptName":"picklistControllerProxy",
                "c0-methodName":"getChildPickLists",
                "c0-id":"0",
                "c0-param0":"string:%7B0%20%3A%20341%7D",
                "c0-param1":"string:school",
                "batchId":"11"
            }

jQuery.ajax({
          ...
          data: JSON.stringify(data),
          ...
});