调用$ .ajax发生故障

时间:2013-12-16 13:30:46

标签: javascript jquery ajax

我正在进行ajax调用,但服务器端代码未被命中。而是控制转到项目中包含的javascript文件。这是我的代码:

<script type="text/javascript">
    function AddEmployee() 
    {
        debugger;
        // jQuery.support.cors = true;
        $.support.cors = true;
        var Product = new Object();
        Product.ID = 10;
        Product.Name = "kRISH";
        Product.Price = "23";
        Product.Category = "AS";
        // console.log(JSON.stringify({ Name: "kRISH", Price: "23", Category: "AS" }));
        var json_text = JSON.stringify(Product,null,2);
        **$.ajax**
        ({
            url: 'http://localhost:62310/api/products',
            type: 'POST',
            data: json_text,
            contentType: "application/json;charset=utf-8",
            success: function (data) { WriteResponse(data); },
            error: function (x, y, z) 
            {
                $('#contentProgress').popup("close");
                alert(x.responseText + " " + x.status);
            }
        });
    }
</script>

1 个答案:

答案 0 :(得分:1)

网址是否正确,为什么我们不在这里使用相对网址。你能试试吗

  $.ajax('http://localhost:62310/api/products', {
        type: "post",
        data: json_text,
        dataType: "json",
        cache: false,
        contentType: "application/json",
        error: function (xhr, textStatus, errorThrown) {
            alert(textStatus);
        }
  });