jQuery Ajax simple call

前端 未结 2 336
忘掉有多难
忘掉有多难 2020-12-01 12:08

I\'m trying a basic ajax call. So I\'m hosting the following test php on a test server: http://voicebunny.comeze.com/index.php?numberOfWords=10 This web page is my own t

相关标签:
2条回答
  • 2020-12-01 12:54

    please set dataType config property in your ajax call and give it another try!

    another point is you are using ajax call setup configuration properties as string and it is wrong as reference site

    $.ajax({
    
        url : 'http://voicebunny.comeze.com/index.php',
        type : 'GET',
        data : {
            'numberOfWords' : 10
        },
        dataType:'json',
        success : function(data) {              
            alert('Data: '+data);
        },
        error : function(request,error)
        {
            alert("Request: "+JSON.stringify(request));
        }
    });
    

    I hope be helpful!

    0 讨论(0)
  • 2020-12-01 13:09

    You could also make the ajax call more generic, reusable, so you can call it from different CRUD(create, read, update, delete) tasks for example and treat the success cases from those calls.

    makePostCall = function (url, data) { // here the data and url are not hardcoded anymore
       var json_data = JSON.stringify(data);
    
        return $.ajax({
            type: "POST",
            url: url,
            data: json_data,
            dataType: "json",
            contentType: "application/json;charset=utf-8"
        });
    }
    
    // and here a call example
    makePostCall("index.php?action=READUSERS", {'city' : 'Tokio'})
        .success(function(data){
                   // treat the READUSERS data returned
       })
        .fail(function(sender, message, details){
               alert("Sorry, something went wrong!");
      });
    
    0 讨论(0)
提交回复
热议问题