Javascript jqueryajaxurl评估

Javascript jqueryajaxurl评估,javascript,jquery,ajax,Javascript,Jquery,Ajax,基于客户端移动/桌面平台,我必须对两个不同的域发出ajax调用: var baseDomain=isMobile?http://m.test.com : http://www.test.com; 函数addProductID,按钮客户端ID{ $.ajax{ 类型:POST,, evalurl:\+baseDomain+/data/call.aspx/AddToShoppingCart\,; contentType:application/json;字符集=utf-8, 数据:{productI

基于客户端移动/桌面平台,我必须对两个不同的域发出ajax调用:

var baseDomain=isMobile?http://m.test.com : http://www.test.com; 函数addProductID,按钮客户端ID{ $.ajax{ 类型:POST,, evalurl:\+baseDomain+/data/call.aspx/AddToShoppingCart\,; contentType:application/json;字符集=utf-8, 数据:{productId:'+productId+',数量:1,isSingle:true}, 数据类型:json, 成功:functiondata{ ProductAddedSuccessdata.d,按钮客户端ID; }, 错误:ProductAddedError }; }
我无法完成此操作,因为我总是得到SyntaxError:缺少形式参数。我错在哪里

URL只是一个字符串,因此在变量中创建所需的字符串,并将其分配给Ajax选项的URL属性:

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
    });
}

错误的原因是您在匿名对象声明的中间放置一个函数调用EVAL! e、 g

这对Javascript来说毫无意义:

试试这个:

$.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
});

为什么要评估?那里不需要动态计算。已计算baseDomain。你不需要对它进行评估。只需使用url:baseDomain+'/data/call.aspx/AddToShoppingCart'不要漏掉逗号,因为有什么特别的原因让你选择两个相同答案中的第二个作为正确答案?因此,礼仪是接受第一个正确答案,悬停回答日期以获得准确的日期/时间。在发布相同的答案之前,请检查是否尚未提供您答案的副本。谢谢:
$.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
});