简体   繁体   English

$ .ajax()成功调用,但不会运行方法

[英]$.ajax() success called, but will not run method

This is really puzzling me. 这真让我感到困惑。 Any idea why this would be happening? 知道为什么会这样吗?

This code does work: 此代码可以正常工作:

 $.ajax({
            type: 'GET',
            dataType: 'json',
            url: 'http://localhost:3235/Users/searchUsers?callback=?&searchString=' + searchString,
            success: alert("Success")
        });

This code does not: 此代码不:

 $.ajax({
            type: 'GET',
            dataType: 'json',
            url: 'http://localhost:3235/Users/searchUsers?callback=?&searchString=' + searchString,
            success: function(data){
              alert("Success");
            }
        });

The first snippet is bad syntax and shouldn't be used, you get the alert because it is called immediately, not on success. 第一个代码段语法错误,不应使用,您会收到警报,因为它会立即调用,而不是成功调用。

There are only two reasons for you to not get the alert with the 2nd snippet. 您没有收到第二个摘要的警报只有两个原因。

  1. the server isn't returning a success status code, for example, it's either 404, 500, etc, not 200. 服务器未返回成功状态代码,例如404、500等,而不是200。
  2. The only other possibility is the jsonp being returned either isn't jsonp, or isn't valid jsonp. 唯一的其他可能性是返回的jsonp不是jsonp或不是有效的jsonp。

Most likely you are confusing JSONP with JSON, so i'll give you an example of each. 您很可能将JSONP与JSON混淆了,因此我将为您提供每个示例。 First is json: 首先是json:

{"foo":"bar"}

and this is JSONP ...?callback=somecallbackname&... : 这是JSONP ...?callback=somecallbackname&...

somecallbackname({"foo":"bar"})

Note, somecallbackname will be supplied by jQuery, you'll have to get the value of the callback get parameter and use it to generate the JSONP accordingly. 注意, somecallbackname将由jQuery提供,您必须获取callback get参数的值,并使用它相应地生成JSONP。

The first one "works" because of a bug in your code. 第一个“工作”是由于代码中的错误。 It is calling the alert and storing what it returns in the success callback. 它正在调用警报并将其返回的内容存储在成功回调中。

Add an error handler to see what the actual problem is. 添加错误处理程序以查看实际问题是什么。

 $.ajax({
            type: 'GET',
            dataType: 'json',
            url: 'http://localhost:3235/Users/searchUsers?callback=?&searchString=' + searchString,
            success: function(data){
              alert("Success");
            },
            error: function (xhr, status, msg) {
              console.log(status, msg);
            }
        }); 

If you can not get the error handler to fire. 如果无法使错误处理程序启动。 Look at the net tab on the JavaScript console, and look at the http request. 查看JavaScript控制台上的net选项卡,然后查看http请求。 You will see the error that the server returns. 您将看到服务器返回的错误。

The alert you're getting from your first code snippet is misleading. 您从第一个代码段收到的警报具有误导性。

When alert("Success") is assigned to the success property your actually immediately calling it. 当将alert("Success")分配给success属性时,您实际上会立即调用它。

You're second approach is the one you want. 第二种方法是您想要的方法。

The first example is wrong - the result of alert("Success") is assigned as the callback, not a function. 第一个示例是错误的alert("Success")被分配为回调,而不是函数。

Your second example is closer, but you are mixing up JSON and JSONP. 您的第二个示例更加接近,但是您正在混合使用JSON和JSONP。 If your service is returning JSON, you should remove callback=? 如果您的服务返回JSON,则应删除callback=? from your request url (actually, you can remove this regardless, setting dataType:'jsonp' will add it automatically). 从您的请求网址(实际上,您可以删除此网址,设置dataType:'jsonp'会自动添加它)。

var ajax_url = 'http://example.com:3235/Users/searchUsers?searchString=' + searchString;

$.ajax({
    type: 'GET',
    dataType: 'json',
    url: ajax_url,
    success: function(data){
        alert("Success");
    },
    error: function(xhr, status, error){
        alert("Error: " + status + " " + error);
    },
    complete: function(xhr, status){
        alert("Complete: " + status);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM