Jsonp没有得到变量userRole

时间:2015-01-27 06:24:32

标签: javascript

我正在尝试编写一个jsonp,它将单个变量的内容 - userRole从一个完全独立的URL中拉出来。我只能在服务器URL上执行javascript,并且这些站点必须保持独立。为什么service.userRole返回undefined?我如何解决它? 服务器:

function service(request, response)
{
var callBackFuncStr = request.getParameter('callback');
var strJson='';
var jsonval = userRole;
strJson=callBackFuncStr+'(\''+jsonval+'\');';
response.write(strJson);
var json=userRole;
var context=  nlapiGetContext();
   var userRole = context.getRole();
   response.write( userRole); 
}

客户端:

<script type="text/javascript"> 
$.ajax({
    url: "********.com/app/site",
    type: "GET",
    dataType : "jsonp",
    success: function( service ) {
        console.log(service.userRole);
    },
    error: function( xhr, status, errorThrown ) {
  console.log( "Sorry, there was a problem!" );
        console.log( "Error: " + errorThrown );
        console.log( "Status: " + status );
        console.dir( xhr );
    }, 
    // Code to run regardless of success or failure
    complete: function( xhr, status ) {
  console.log( "The request is complete!" );
    }
});
</script>

2 个答案:

答案 0 :(得分:0)

您的服务器只返回一个字符串,而不是具有userRole属性的对象。所以它应该是:

success: function(userRole) {
    console.log(userRole);
},

或者您可以修复服务器以便返回一个对象。应该这样做:

strJson=callBackFuncStr+'({"userRole": "'+jsonval+'"});';

答案 1 :(得分:0)

service功能的逻辑搞乱了。

var callBackFuncStr = request.getParameter('callback');
// callBackFuncStr is now `callback716242`, or whatever jQuery
// said it will be
var strJson='';
// strJson is now an empty string
var jsonval = userRole;
// userRole is declared in the current function but not yet
// defined, so it will give you a value of `undefined`. Thus,
// `jsonval` also becomes `undefined`.
strJson=callBackFuncStr+'(\''+jsonval+'\');';
// The `strJson` now becomes "callback716242('undefined')".
// This would probably be wrong thing to do even if `jsonval`
// was correct in the first place.
response.write(strJson);
// You send `callback716242('undefined')` to the client.
// If this were all, the client would receive the string
// `"undefined"` as the JSONP response. Even if it was a valid
// user role, a string does not have a `.userRole` property.
// But that is not all...
var json=userRole;
// `json` is now also `undefined`.
var context=  nlapiGetContext();
   var userRole = context.getRole();
// Only now does `userRole` get a value (say, `admin`)
   response.write( userRole); 
// Now you write once more to the client; the client will now
// receive `callback716242('undefined')admin`, which should
// generate a JavaScript error. \o/

要解决这个问题,让我们重新安排你的功能。

function service(request, response) {
  var callBackFuncStr = request.getParameter('callback');
  var context = nlapiGetContext();
  var userRole = context.getRole();                // "admin"
  var payload = { userRole: userRole };            // { userRole: "admin" }
  var json = JSON.stringify(payload);              // "{userRole:\"admin\"}"
  var jsonp = callBackFuncStr + "(" + json + ")";  // "callback716242({userRole:\"admin\"})"
  response.write(jsonp);
}