weixin_33711647 2015-12-02 15:46 采纳率: 0%
浏览 37

jQuery Ajax网址评估

I have to fire an ajax call against two different domain based on the platform of the client (mobile/desktop):

var baseDomain = (isMobile()) ? "http://m.test.com" : "http://www.test.com";

function AddProduct(ProductId, ButtonClientId) {
    $.ajax({
        type : "POST",
        eval("url : \""+baseDomain+"/data/call.aspx/AddToShoppingCart\",");
        contentType : "application/json; charset=utf-8",
        data : '{productId:' + ProductId + ', quantity: 1, isSingle: true}',
        dataType : "json",
        success : function(data) {
            ProductAddedSuccess(data.d, ButtonClientId);
        },
        error : ProductAddedError
    });
}

I cannot go thru this as I always get the "SyntaxError: missing formal parameter". Where I'm wrong?

  • 写回答

2条回答 默认 最新

  • weixin_33725239 2015-12-02 15:51
    关注

    The URL is just a string, so create the string you want in a variable and assign it to the url property of the Ajax options:

    var baseDomain = isMobile() ? "http://m.test.com" : "http://www.test.com";
    
    function AddProduct(ProductId, ButtonClientId) {
        $.ajax({
            type : "POST",
            url: baseDomain + "/data/call.aspx/AddToShoppingCart",
            contentType : "application/json; charset=utf-8",
            data : '{productId:' + ProductId + ', quantity: 1, isSingle: true}',
            dataType : "json",
            success : function(data) {
                ProductAddedSuccess(data.d, ButtonClientId);
            },
            error : ProductAddedError
        });
    }
    

    The reason for the error is you are placing a function call (to eval()) in the middle of an anonymous object declaration!

    e.g.

    {
       propName1: "Value 1",
       someFunctionCall(),
       propName2: "Value 3"
     }
    

    Which makes no sense to Javascript :)

    评论

报告相同问题?

悬赏问题

  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真