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 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办